Optimize loading of remote plist

£可爱£侵袭症+ 提交于 2020-01-01 19:30:06

问题


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.

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?

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];

EDIT: 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.

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.");
}   

回答1:


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

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').

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




回答2:


You can minimize load time only by minimizing the plist itself. Try various formats (binary plist, xml plist). If you have an option - try fetching json. AFAIK it has best information-to-size ratio. Binary plist parses fastest and xml plist is best for debugging purposes.



来源:https://stackoverflow.com/questions/5016261/optimize-loading-of-remote-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!