优化远程plist的加载 [英] Optimize loading of remote plist

查看:137
本文介绍了优化远程plist的加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个应用程序,它从我的服务器上的plist中加载信息(我希望Apple可以这样做:-))。该应用程序在WiFi上运行顺畅,但在3G上使用时,加载时间有点太长。因此,我想优化我的plist的加载。

I have written an application which loads information from a plist which lies on my server (I hope this is OK with Apple :-)). The application runs smooth on WiFi but when using it on 3G the load times are a little too long. Therefore I would like to optimize the loading of my plist.

我想我可以在设备上存储plist的副本,只检查远程plist是否已更改如果是这样,请下载它。我不确定这是否会减少数据量,从而最大限度地减少加载时间,我甚至不确定如何用代码编写它。

I imagine that I could store a copy of the plist on the device and only check if the remote plist has changed and if so, download it. I am not sure if this will be less data and therefore minimize the load time and I'm not even sure how this would be written in code.

有没有人有想法如何最小化加载时间并可能发布代码示例?

Does anyone have an idea how I can minimize the loadtime and possibly post a code sample?

现在我使用这两行来加载plist并存储它。 FETCH_URL 显然是我的网址。

Right now I am using these two lines to load the plist and store it. FETCH_URL is obviously my URL.

NSURL *url = [NSURL URLWithString:FETCH_URL];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfURL:url];

编辑:我正在尝试使用NSURLConnection,因为托马斯克莱森建议但我似乎无法弄清楚如何将数据转换为NSMutableArray。

I am trying to use NSURLConnection as Thomas Clayson suggests but I can't seem to figure out how to convert the data into an NSMutableArray.

这是在我的viewDidLoad中:

This is in my viewDidLoad:

NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:FETCH_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        NSLog(@"Connection open.");
    }
    else {
        NSLog(@"Failed connecting.");
    }

然后我有这两种方法来检查是否有任何新数据并且检查是否所有数据都已收集。

Then I have these two methods to check if there is any new data and to check if all data has been collection.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"More data.");
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Connection successful.");
}   


推荐答案

我明白你的意思。您将需要使用NSURLConnnection而不是 initWithContentsOfURL

I understand what you mean. You will want to use NSURLConnnection instead of initWithContentsOfURL.

initWithContentsOfURL是同步的。这意味着它将在主线程上运行,从而挂起应用程序(这是因为UI事件必须在主线程上发生,如果进程正在发生(等待plist下载),应用程序将停止 )。

initWithContentsOfURL is synchronous. That means that it will run on the main thread and thus "hang" the application (this is because UI events have to occur on the main thread and if a process is happening (waiting for the plist to download) the app will 'stop').

无论如何 - 如果您使用NSURLConnection,那么您将能够异步(在另一个线程上)。这意味着有一段时间您的应用程序将可用,但不会立即显示来自plist的数据。因此,当 connectionDidFinishLoading:

Anyway - if you use NSURLConnection then you will be able to do it asynchronously (on another thread). This means that for a while your app will be usable, but won't display the data from the plist straight away. So you'll need to handle the view to refresh the data when the connectionDidFinishLoading:.

无论如何都需要处理视图以刷新数据。 :)非常有用

look it up anyway. :) its very useful

这篇关于优化远程plist的加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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