我想在 Core Data 中使用多线程。我在 s 中解析 xml 文件NSManageObject
。我使用下面的代码,我得到运行时错误Can only use -performBlock: on an NSManagedObjectContext that was created with a queue
。怎么了?
//xmlParser
- (void)main
{
dispatch_queue_t queueB = dispatch_queue_create("Create Books", NULL);
dispatch_async(queueB, ^{
// Opening xml
// ...
NSManagedObjectContext* context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:mainContext].persistentStoreCoordinator];
[context performBlock:^{
// ...
[self _parseNode:container_node appendTo:books inContext:context];
// ...
NSError* error = nil;
[context save:&error];
[mainContext performBlock:^{
NSError* parentError = nil;
[mainContext save:&parentError];
}];
}];
[context release];
});
dispatch_release(queueB);
}
- (int)_parseNode:(axlNode*)inode appendTo:(NSMutableArray*)ioarray inContext:(NSManagedObjectContext*)context
{
// ...
[context executeFetchRequest:request error:&error];
//...
}