Core Data and threads / Grand Central Dispatch

后端 未结 6 1309
臣服心动
臣服心动 2020-11-29 15:44

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 C

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 16:20

    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];
    

提交回复
热议问题