核心数据和线程/Grand Central Dispatch [英] Core Data and threads / Grand Central Dispatch
问题描述
我是 Grand Central Dispatch (GCD) 和 Core Data 的初学者,我需要您的帮助来将 Core Data 与 CGD 结合使用,以便在我向 Core Data 添加 40.000 条记录时不会锁定 UI.
I'm a beginner with Grand Central Dispatch (GCD) and Core Data, and I need your help to use Core Data with CGD, so that the UI is not locked while I add 40.000 records to Core Data.
我知道CD不是线程安全的,所以我必须使用另一个上下文,然后保存数据并合并上下文,这是我从一些文章中能够理解的.
I know that CD is not thread safe, so I have to use another context, and then save the data and merge contexts, as far as I was able to understand from some articles.
我还不能做的是把各个部分放在一起.
What I couldn't do yet, is put the pieces together.
所以,在我的代码中,我需要你的帮助来解决这个问题.
So, in my code, I need your help on how to to that.
我有:
/*some other code*/
for (NSDictionary *memberData in arrayWithResult) {
//get the Activities for this member
NSArray *arrayWithMemberActivities = [activitiesDict objectForKey:[memberData objectForKey:@"MemberID"]];
//create the Member, with the NSSet of Activities
[Members createMemberWithDataFromServer:memberData
andActivitiesArray:arrayWithMemberActivities
andStaffArray:nil
andContactsArray:nil
inManagedObjectContext:self.managedObjectContext];
}
如何将其转换为在后台工作,然后在完成保存后保存数据并更新 UI,同时保存 40.000 个对象时不会阻塞 UI?
How can I transform this to work on the background, and then, when done saving, save the data and update the UI, without blocking the UI while saving the 40.000 objects?
推荐答案
这里有一个很好的例子供您尝试.如果您有任何问题,请随时回来:
Here's a good example for you to try. Feel free to come back if you have any questions:
self.mainThreadContext... // This is a reference to your main thread context
NSPersistentStoreCoordinator *mainThreadContextStoreCoordinator = [self.mainThreadContext persistentStoreCoordinator];
dispatch_queue_t request_queue = dispatch_queue_create("com.yourapp.DescriptionOfMethod", NULL);
dispatch_async(request_queue, ^{
// Create a new managed object context
// Set its persistent store coordinator
NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];
[newMoc setPersistentStoreCoordinator:mainThreadContextStoreCoordinator]];
// Register for context save changes notification
NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
[notify addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:newMoc];
// Do the work
// Your method here
// Call save on context (this will send a save notification and call the method below)
BOOL success = [newMoc save:&error];
if (!success)
// Deal with error
[newMoc release];
});
dispatch_release(request_queue);
并响应上下文保存通知:
And in response to the context save notification:
- (void)mergeChanges:(NSNotification*)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.mainThreadContext mergeChangesFromContextDidSaveNotification:notification waitUntilDone:YES];
});
}
并且不要忘记在完成后台线程上下文后从通知中心移除观察者.
And don't forget to remove the observer from the notification center once you are done with the background thread context.
[[NSNotificationCenter defaultCenter] removeObserver:self];
这篇关于核心数据和线程/Grand Central Dispatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!