Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:03:57+00:00 2026-05-24T09:03:57+00:00

My Cocoa/Application has a Managed Object Context on the main thread. When I need

  • 0

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 data from a server
  3. Create a new Managed Object Context
  4. Send a notification to the main thread in order to merge the two context

This is the function that receive the notification on the main thread

- (void)loadManagedObjectFromNotification:(NSNotification *)saveNotification
{
    if ([NSThread isMainThread]) {
        [self.managedObjectContext mergeChangesFromContextDidSaveNotification:saveNotification];
    } else {
        [self performSelectorOnMainThread:@selector(loadManagedObjectFromNotification:) withObject:saveNotification waitUntilDone:YES];     
    }
}

I do not receive any error.
My problem is the merge result, it actually concatenate Managed Objects from both context.

My Entity are a really simple list of attribute and relationship.

Maybe the merge need some instructions in order to understand when an updated Managed Object IS NOT a new one, but a edited version of the first one.
I imagine that somewhere I need to specify a way to univocally identify an Entity, (an attribute for example can act like an ID) and something like a merge policy (if 2 managed object represent the same object, take the one with the lastModificationDate more recent).

I just need to understand how to correctly merge the 2 contexts in order to have a single updated copy for each object.

UPDATE 1

The problem is now clear to me.
The 2 context has a big difference: the ObjectID.
While the context on the main thread fetched the ManagedObjects with the Persistent Store coordinator, the second thread create those object by fetching a remote URL. Even if the objects have the same contents, they will have 2 different objectID.

My objects had already an unique identificator, I could use setObjectId in order to set this value. (Apple documentation says this is NOT a good idea).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:03:57+00:00Added an answer on May 24, 2026 at 9:03 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a document-based Cocoa application that has to start up a sub-process before
I've got a small Cocoa problem. I have a StatusBar application that has an
I am developing a cocoa application that will be making heavy use of both
I have a Cocoa application that records datestamps on events. I need to know
I have a cocoa application which has certain subroutines / methods. So if I
I have a cocoa application that has a finder like feel it and is
My document-based Cocoa application uses a NSOutlineView/NSTreeController combo, bound to the document's Core Data
I have small COCOA Mac OS application to play with core data. I have
I am creating an application that has to interact with server data and then
The run loop for the secondary thread of my application is below. It has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.