我的模型对象应该负责下载自己的资源吗? [英] Should my model objects be responsible for downloading their own resources?

查看:63
本文介绍了我的模型对象应该负责下载自己的资源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Myapp使用远程API更新其数据,并且它必须脱机工作,因此在显示应用程序的主要部分之前,必须先下载所有数据.

Myapp uses a remote API to update its data, and it has to work offline, so all the data has to be downloaded before showing the main section of the app.

API返回一个大的JSON结构,代表应用程序的内容. JSON转换为许多字典和数组,然后实例化模型类并由这些字典和数组支持.

The API returns one big JSON structure representing the content of the app. The JSON is converted to many dictionaries and arrays, then model classes are instantiated and are backed by these dictionaries and arrays.

我的问题是,这些词典和数组中的某些包含用于图像的URL,这些URL也必须下载.每个模型对象都有责任下载自己的图像吗?还是应该有某种控制器类来处理此问题?

My problem is that some of these dictionaries and arrays contain URLs for images that also have to be downloaded. Should it be the responsibility of each model object to download its own images? Or should I have some kind of controller class that handles this?

此外,什么是处理此问题的好方法?例如,我怎么知道所有模型对象何时都完成了资源的下载?

Also, what would be a good approach to handle this? For example, how do I know when all model objects have finished downloading their resources?

所有资源必须预先上传,因为客户端希望能够在没有互联网连接的情况下使用该应用程序.

All the resources must be uploaded upfront as the client would like to be able to use the app without an internet connection.

推荐答案

答案取决于您应用的业务需求.

The answer depends upon the business requirements of your app.

  1. 通常一个人会预先下载JSON数据,但会延迟图像的加载,直到UI要求时才下载图像(一种称为延迟加载"的模式).一种特别优雅的处理方式是使用执行实时异步图像检索的UIImageView类别( SDWebImage 是一个很好的实现;如果您使用的是 AFNetworking ,它也有一个) .对于这两种方式,您只需执行以下操作即可:

  1. Often one will download the JSON data up front, but defer the loading of the images, not downloading them until required by the UI (a pattern known as "lazy loading"). One particularly elegant way of handling that is to use a UIImageView category that performs just-in-time asynchronous image retrieval (SDWebImage is a pretty good implementation; if you're using AFNetworking, it has one, too). For both of those, you'd simply do something like:

[self.imageView setImageWithURL:url];

这将异步检索图像,以防UI再次需要时缓存图像,等等.此外,如果在具有单元格重用的表视图中使用此图像,它将取消对先前已下载的先前单元格的旧请求尚未完成确保可见单元图像的请求不会在用户可能不再关心的一堆旧请求之后被积压,等等.

And that will retrieve the image asynchronously, cache the image in case your UI needs it again, etc. Also, if you use this within a tableview with cell reuse, it will cancel old requests for prior cells for which the download hasn't yet finished to make sure that requests for the images for visible cells don't get backlogged behind a bunch of old requests that the user may no longer care about, etc.

有时候,您确实确实想检索所有内容(例如,您要下载一本杂志的内容,以使其可以脱机阅读).

Sometimes, though, you really want to retrieve everything up front (e.g. you're downloading the content for a magazine that you want to make available for reading offline).

在这种情况下,您可能没有单独的数据控制器来下载和解析结果,而不是由模型本身来进行检索.如果需要在完成时通知UI,则可以使用本地通知.

In that case, rather than having the model, itself, do the retrieval, you might have a separate data controller which will download and parse the results. If you need to inform the UI when this is done, you might employ local notifications, to do so.

虽然后一种模式乍看之下似乎很吸引人,但它有许多缺点.例如,如果用户正在其蜂窝连接上,则可能会花费大量带宽下载用户可能不会立即需要的图像,从而在此过程中消耗了用户的数据计划和电池.因此,您可能只想在以下情况下这样做:(a)确实需要离线使用所有东西;并且(b)您可以接受由此带来的资源消耗(数据计划和电池消耗).

While this latter pattern might seem appealing at first glance, it has numerous disadvantages. If, for example, the user is on their cellular connection, you might spend a lot of bandwidth downloading images that the user might not immediately need, consuming the user's data plan and battery in the process. So you might want to only do this if (a) you really need everything up front for offline usage; and (b) you're ok with the resource (data plan and battery consumption) drain this entails.

这篇关于我的模型对象应该负责下载自己的资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆