从http://goo.gl/MkV8V是ThreadedCoreData示例(来自Apple)在主线程上创建NSManagedObjectContext?
您必须在将要使用的线程上创建托管上下文。如果您使用NSOperation,请注意其init方法在与调用方相同的线程上调用。因此,您不得为队列的init方法中的队列创建托管对象上下文,否则它将与调用方的线程关联。相反,你应该在main(对于一个串行队列)或start(对于一个并发队列)中创建上下文。
在ConnectionDidLoading方法:
ParseOperation *parseOperation = [[ParseOperation alloc] initWithData:self.earthquakeData];
[self.parseQueue addOperation:parseOperation];
[parseOperation release]; // once added to the NSOperationQueue it's retained, we don't need it anymore
ConnectionDidiLoading被称为主线程上。现在里面的ParseOperation :: initWIthData方法,我们有这样的事情:(见ParseOperation.m文件)
// setup our Core Data scratch pad and persistent store
managedObjectContext = [[NSManagedObjectContext alloc] init];
[self.managedObjectContext setUndoManager:nil];
SeismicXMLAppDelegate *appDelegate = (SeismicXMLAppDelegate *)[[UIApplication sharedApplication] delegate];
[self.managedObjectContext setPersistentStoreCoordinator:appDelegate.persistentStoreCoordinator];
#
我的理解是,这managedObjectContext仍然是主要的线程上创建。
如果有人澄清或纠正我的理解,将不胜感激,因为Apple的示例代码不太正确。