在后台线程中加载CoreData [英] Loading CoreData in a background thread

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

问题描述

我觉得我今天已经阅读了Stack Overflow上的每个CoreData问题,但仍然非常困惑。 :)

I feel like I've read every CoreData question on Stack Overflow today, and am still very very stuck. :)

我正在开发一个使用CoreData的应用程序,该应用程序基于斯坦福大学cs193p讲座14(Photomania应用程序)中说明的方法。它使用实现NSFetchedResultsController委托的UITableViewController子类,并且当然,表在获取结果时自动更新。

I'm working on a application that uses CoreData that is based on the methods illustrated in the Stanford University cs193p Lecture 14 (the Photomania app). It uses a UITableViewController subclass that implements the NSFetchedResultsController delegates, and of course the table updates automatically as results are fetched.

一切正常,但是在文档中填充数据时,UI会阻塞,因为它发生在主线程(即文档的managedObjectContext)中。我已经在后台线程中下载了数据,这只是实际填充导致阻塞的NSManagedObjects的代码。该讲座暗示使用NSManagedObjectContext的Parent上下文,以便在后台加载Document,然后重新提取主线程中的数据以填充表。我几乎可以正常工作(我认为),除非我经常在表中得到两次输入。似乎[self.tableView beginUpdates] / [self.tableView endUpdates]可以解决,但由于我正在将NSManagedObjectContext保存在后台上下文中,所以我不知道该放在哪里。

Everything works but the UI blocks when the Document is populated with data, because it occurs in the Main thread (which is the document's managedObjectContext). I'm already downloading the data in a background thread, this is just the code that actually populates the NSManagedObjects that is causing the blocking. The lecture alludes to using the NSManagedObjectContext's Parent context in order to load up the Document in the background and then "refetch" the data in the main thread to populate the table. I almost have things working (I think) except I often get double entries in my table. It seems like the sort of thing [self.tableView beginUpdates] / [self.tableView endUpdates] would resolve, but because I'm doing the NSManagedObjectContext save in the background context I don't know where I would put it.

我也可能会以完全错误的方式来解决这个问题。 :)无论如何,这是相关代码:

I may also be going about this the entirely wrong way. :) In any event, here is the relevant code:

NSManagedObjectContext *backgroundContext;
backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
// document is my UIManagedDocument
backgroundContext.parentContext = document.managedObjectContext; 

[backgroundContext performBlockAndWait:^{             
     // Do stuff here to populate the document.             
     [backgroundContext save:nil];
}];


推荐答案

嗯,不知道为什么,但这解决了我的问题: https://stackoverflow.com/a/9451450/314051 。具体而言,就在[backgroundContext保存]之前:

Well, not sure why, but this resolved my issue: https://stackoverflow.com/a/9451450/314051. Specifically, right before the [backgroundContext save]:

NSSet *inserts = [backgroundContext insertedObjects]; 
[backgroundContext obtainPermanentIDsForObjects:[inserts allObjects] error:&error]; 

我需要做一些研究才能弄清楚原因。感谢您的建议,这有助于我确定这不是UI问题。

I'll need to do some research to understand exactly why. Thanks for the suggestions, which helped me determine this wasn't a UI issue.

这篇关于在后台线程中加载CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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