Core Data merge two Managed Object Context

后端 未结 1 1082
野趣味
野趣味 2020-12-12 16:40

My Cocoa/Application has a Managed Object Context on the main thread. When I need to update my data my program will:

  1. Start a new thread
  2. Receive new d
相关标签:
1条回答
  • Here is what you need to do in order to correctly merge the contexts. First, you do not need your own notification. Performing a save operation on a context automatically forwards the following notification to registered observers:

    NSManagedObjectContextDidSaveNotification
    

    Therefore, all you need to do is:

    1) in your main thread, may be in the viewDidLoad method, register for this notification:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(contextDidSave:)
                                                 name:NSManagedObjectContextDidSaveNotification
                                                object:nil];
    

    2) implement the contextDidSave: method in your main thread as follows:

    - (void)contextDidSave:(NSNotification *)notification
    {
    
        SEL selector = @selector(mergeChangesFromContextDidSaveNotification:); 
        [managedObjectContext performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES];
    
    }
    

    3) in your dealloc method add the following:

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    4) create a new context in your other thread using something like the following method:

    - (NSManagedObjectContext*)createNewManagedObjectContext
    {
    
        NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; 
        [moc setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
        [moc setUndoManager:nil];
        return [moc autorelease];
    }
    

    5) upon receiving the new data, the proper way to handle this situation is the use of managed object IDs. Since managed object IDs are thread safe, you can pass them from your main thread to the other thread, then use existingObjectWithID:error: to retrieve the object associated to a specific ID, update it and save the context. Now the merge will operate as you expect. Alternatively, if you do not know in advance what managed object IDs must be passed between the threads, then in your other thread you simply fetch the objects using a predicate to retrieve the ones corresponding to the objects retrieved from the server, then you update them and save the context.

    0 讨论(0)
提交回复
热议问题