简体   繁体   English

ios App通过预先请求数据来提高网络性能

[英]ios App improve network performance by requesting data up front

I have an application that I'm writing that pulls data from a few network sources: 1) list of blog posts (UITableViewController) 2) list of videos (UIViewController with an embedded UIScrollView) 3) list of images (UIViewController with an embedded UIScrollView) 我正在编写一个应用程序,该应用程序从一些网络来源中提取数据:1)博客帖子列表(UITableViewController)2)视频列表(具有嵌入式UIScrollView的UIViewController)3)图像列表(具有嵌入式UIScrollView的UIViewController )

Right now, there is a home screen with a menu and when you push one of the buttons, a destinationViewController (described above) is what loads the data on demand. 现在,有一个带有菜单的主屏幕,当您按下其中一个按钮时,destinationViewController(如上所述)是按需加载数据的工具。 I've noticed this is quite slow, especially when on a cellular data connection as opposed to wifi. 我注意到这很慢,尤其是在使用蜂窝数据连接而不是wifi时。

I was thinking about creating a class that requests all the data up front and kick it off every time the app is reentered. 我当时正在考虑创建一个类,该类预先请求所有数据,并在每次重新输入应用程序时将其启动。 Does anyone have suggestions that could help me answer the following? 有没有人建议可以帮助我回答以下问题?

1) are there any classes, frameworks, or existing i code i can use to kick off these requests in a single place? 1)我可以使用任何类,框架或现有的i代码在一个地方启动这些请求吗? 2) how do my destination view controllers (mentioned above) get the data? 2)我的目标视图控制器(如上所述)如何获取数据? 3) how do my destination view controllers get informed that the data is ready if they happen to be invoked before the data is available? 3)如果我的目标视图控制器碰巧在数据可用之前被调用,如何通知我数据已经准备好? 4) is there a better strategy i should employ? 4)我应该采用更好的策略吗?

I appreciate the help. 感谢您的帮助。

Thanks, jas 谢谢,贾斯

1) RestKit is generally regarded as one of the the standard web interaction frameworks http://restkit.org 1)RestKit通常被视为标准的Web交互框架之一http://restkit.org

2) RestKit provides methods such as -[getObjectsAtPath: parameters: success: failure:] or post, which will provide the response in success. 2)RestKit提供了-[getObjectsAtPath: parameters: success: failure:]或post之类的方法,它们将提供成功的响应。 Additionally, it can convert the response directly into the respective objects for you with mapping. 此外,它可以通过映射将响应直接转换为各个对象。

3) Generally I would post a notification which is unique to that request, and any controller interested would get notified, with the response located on notification.object within the listener. 3)通常,我会发布一个对该请求唯一的通知,任何感兴趣的控制器都将收到通知,响应位于侦听器内的notification.object Additionally most network requests provide for a callback handler where you could update the UI directly. 另外,大多数网络请求都提供了一个回调处理程序,您可以在其中直接更新UI。

4) I would advise against preemptive loading since you're using resources for something that you may not actually use. 4)我建议不要先占式加载,因为您将资源用于可能不实际使用的东西。 My advice would be to breakdown your calls to the smallest possible level, then as the notifications are posted, that data would be inserted into the UI. 我的建议是将您的呼叫分解到最小的级别,然后在发布通知时,将数据插入到UI中。

Off the top of my head, I would make the request class you mentioned and start all the request methods in applicationDidFinishLaunching in AppDelegate. 让我烦恼的是,将创建您提到的请求类,并在AppDelegate的applicationDidFinishLaunching中启动所有请求方法。 I would also probably make custom NSObjects for each type of object you would be fetching and in each of your request class methods, convert the fetched data into said object, then cache each object to disk as they are downloaded. 我可能还会为要获取的每种对象类型创建自定义NSObject,并在每个请求类方法中,将获取的数据转换为所述对象,然后在下载每个对象时将其缓存到磁盘中。 Then in your viewcontrollers, fetch the cached objects as needed. 然后在您的视图控制器中,根据需要获取缓存的对象。

When you are cacheing, make sure you cache each object with a key that will 100% be unique, because you are going to want to run a check on the current local cache before you start a new download. 缓存时,请确保使用每个100%唯一的密钥缓存每个对象,因为在开始新的下载之前,您将要对当前本地缓存进行检查。 I would probably string together the file type and file name, and set that string as the key for the cached object. 我可能会将文件类型和文件名串在一起,然后将该字符串设置为缓存对象的键。

To run the check on current cache in your request class methods, something that says "if current cache contains object for key: uniqueKey... do nothing. If else, start the download and cache the object when finished." 要在请求类方法中对当前缓存进行检查,请显示“如果当前缓存包含键对象:uniqueKey ...,则不执行任何操作。否则,请开始下载并在完成后缓存对象。”

Also run a check in your view controllers, because you're also going to want to handle the case where your view controller is requesting a cached object, but it hasn't downloaded yet. 另外,还要在视图控制器中运行检查,因为您还将要处理视图控制器请求缓存对象但尚未下载的情况。 So something along the lines of "if current cache has object for key:key, great! use it. If else, start the download... cache it... then give me a call back here so i can use it while I display a loading message to the end user." 因此,类似“如果当前缓存中有key:key的对象,太好了!使用它。否则,请开始下载...缓存它...然后给我回电,以便我可以在使用时使用它向最终用户显示加载消息。”

Im sure there are other scenarios that you are going to have to deal with, but that's the theoretical direction I would head in. 我肯定还有其他情况需要处理,但这是我要遵循的理论方向。

EDIT: 编辑:

check out this, it will probably help you a lot. 看看这个,它可能对您有很大帮助。 I think it also uses Ego Cache (if you dont want to write your own cache methods): https://github.com/Infusion-apps/Download-Manager 我认为它也使用Ego缓存(如果您不想编写自己的缓存方法): https : //github.com/Infusion-apps/Download-Manager

EDIT 2: I also agree with @RyanDignards point #4. 编辑2:我也同意@RyanDignards点#4。 If possible, avoid fetching data you don't need. 如果可能,请避免获取不需要的数据。 However, only you really know your UI/UX and app functionality, and my suggestion is assuming there is a good chance your end user is going to be using your app for the sole purpose of consuming the content you provide. 但是,只有您真正了解您的UI / UX和应用程序功能,我的建议是假设您的最终用户很有可能将使用您的应用程序仅仅是出于消费您提供的内容的目的。 So they are most likely going to be wanting to read the blog posts, watch the videos etc... The call is up to you, if you think there is more of a chance that the user will be viewing all the content than not, I would go the preload route, because nothing pisses off a user like having to wait. 因此,他们最有可能想要阅读博客文章,观看视频等。来电取决于您,如果您认为用户有更多机会查看所有内容,我会选择预加载路线,因为没有什么比烦恼更让用户生气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM