Load a big plist from url in background

我的未来我决定 提交于 2019-12-24 03:02:08

问题


I load a big plist file from url and I must wait for some seconds before I can use the application. Is there some solution? How it can be loaded in background? Is GCD what I need? How it can be implemented?

My code:

NSString *urlStr = [[NSString alloc] 
                    initWithFormat:@"http://www.domain.com/data.xml"];

NSURL *url = [NSURL URLWithString:urlStr];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url];

回答1:


You could create a new thread to do so Please check the following

//Create the thread 
[NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

- (void) loadPList
{
    //Load the plist
    NSString *urlStr = [[NSString alloc] 
                       initWithFormat:@"http://www.domain.com/data.xml"];

    NSURL *url = [NSURL URLWithString:urlStr];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url];

    //Update the ui
    dispatch_async(dispatch_get_main_queue(), ^{
        //Update UI if you have to
    });

}


来源:https://stackoverflow.com/questions/10861935/load-a-big-plist-from-url-in-background

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