Core Data通过新的NSPrivateQueueConcurrencyType获取背景 [英] Core Data background fetching via new NSPrivateQueueConcurrencyType

查看:221
本文介绍了Core Data通过新的NSPrivateQueueConcurrencyType获取背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在在iOS5中真的很简单吗?

Is it really so simple now in iOS5?

我在AppDelegate中使用这个代码执行背景撷取:

I used to perform a background fetch using this code in my AppDelegate:

dispatch_queue_t downloadQueue = dispatch_queue_create("DownloadQueue", NULL);
dispatch_async(downloadQueue, ^{
        self.myDownloadClass = [[MyDownloadClass alloc]initInManagedObjectContext:self.managedObjectContext];
        [self.myDownloadClass download];
    });

dispatch_release(downloadQueue);

我的下载类执行NSURLConnection来获取一些XML数据,使用NSXMLParser来解析数据,然后更新核心数据中的复杂模式。我总是切换到主线程来实际更新核心数据。 Messy代码,有很多dispatch_sync(dispatch_get_main_queue()....调用。

My download class performs an NSURLConnection to fetch some XML data, uses NSXMLParser to parse the data, and then updates a complex schema in core data. I would always switch to the main thread to actually update the core data. Messy code, with lots of calls to dispatch_sync(dispatch_get_main_queue()....

我的新代码看起来像这样:

My new code looks like this:

NSManagedObjectContext *child = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[child setParentContext:self.managedObjectContext];

[child performBlock:^{
    self.myDownloadClass = [[MyDownloadClass alloc]initInManagedObjectContext:child];
    [self.myDownloadClass download];
    }];

以及我的AppDelegate中一些其他代码的一个小改变,将父模型对象上下文类型设置为NSMainQueueConcurrencyType:

along with a small change to some other code in my AppDelegate to set the parent model object context type to NSMainQueueConcurrencyType:

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

它似乎工作得很好。整个更新过程仍然在单独的线程中运行,但我不必创建线程。看起来像魔法。

It seems to work very well. The entire update process still runs in a separate thread, but I did not have to create a thread. Seems like magic.

只要记住,如果你想提交对物理核心数据文件的更改,你在父上下文中调用save:

Just remember if you want to commit your changes to the physical core data files, you have call save: on the parent context as well.

我没有在这里问一个问题。我发布这个,所以它帮助别人,因为我在搜索新的iOS5管理对象上下文方法时发现的所有东西只给出高级别的细节,没有代码示例和所有其他搜索在后台抓取核心数据是旧的,有时很老,并讨论如何在iOS5之前做它。

I didn't really ask a question here. I'm posting this so it helps others because everything I found when searching for the new iOS5 managed object context methods only gave high level details with no code examples And all the other searches for fetching core data in the background are old, sometimes very old, and discuss how to do it pre-iOS5.

推荐答案

是的 - 这是真的很容易现在对于iOS 4兼容性,先前的障碍仍然存在,但是文档在线程限制上并不太糟糕。也许你应该添加到维基部分?

Yes - it is really that easy now (in iOS 5.0). For iOS 4 compatibility, the previous hurdles remain, but the documentation is not too bad on thread confinement. Maybe you should add this to a wiki section?

这篇关于Core Data通过新的NSPrivateQueueConcurrencyType获取背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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