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

  • SEARCH
  • Home
  • 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 3978732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:05:02+00:00 2026-05-20T05:05:02+00:00

I have read Marcus Zarra’s chapter on multithreading in his Core Data book and

  • 0

I have read Marcus Zarra’s chapter on multithreading in his Core Data book and have looked fairly closely at his sample code. But his code and others that I have found elsewhere seem to be focused in background processes that do not need to be aware of each other. These examples are good for importing a tree structure – but they do not address the importing of a more general (complex) structure, like a directed acyclic graph.

In my case, I am trying to parse a C++ class hierarchy and would like to use as many NSOperations as possible. I would like to create an NSManagedObject instance for each encountered class and I would like to merge different NSManagedObjectContexts whenever one is saved.

As an aside: I am able to get things working with a single NSOperation that iterates of files and parse one at a time. In this implementation, the -mergeChanges: approach that calls -mergeChangesFromContextDidSaveNotification: on main thread’s MOC works well.

But ideally, I would have one NSOperation iterate over source files and spawn NSOperations to parse each file. I have tried several approaches – but can’t seem to get it right. The most promising was to have each NSOperation observing NSManagedObjectContextDidSaveNotification. With -mergeChanges: looking like this:

- (void) mergeChanges:(NSNotification *)notification
 {
 // If locally originated, then trigger main thread to merge.
 if ([notification object] == [self managedObjectContext])
  {
  AppDelegate *appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
  NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];

  // Merge changes into the main context on the main thread
  [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
         withObject:notification
         waitUntilDone:YES];
  return;
  }
   // If not locally originated, then flag need to merge with in this NSOperation's thread.  
 [self setNeedsToMerge:YES];
 [self setMergeNotification:notification];
 }

Essentially, the parsing NSOperation’s main() checked ivar ‘needsToMerge’ periodically. If it was true, then -mergeChangesFromContextDidSaveNotification: was called on local MOC with cached NSNotifications. And then needsToMerge was reset. If notification had originated locally, then main thread was told to perform -mergeChangesFromContextDidSaveNotification: on its MOC.

I am sure that there is a good reason why this didn’t work and why I get this:

warning: Cancelling call – objc code
on the current thread’s stack makes
this unsafe.

I have also tried to use NSPeristentStoreCoordinator’s lock to control access – but this is problematic if it is held during a call to NSManagedObjectContext’s -save: method because -save: will notify interested observers of save event and -mergeChangesFromContextDidSaveNotification: appears to block trying to acquire PSC’s lock.

It just seems that this should be a lot easier.

  • 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-20T05:05:03+00:00Added an answer on May 20, 2026 at 5:05 am

    I think I hade the same problem and here is how I solve it:

    Create a custom NSOperation Class where you define:

    NSMutableArray * changeNotifications;
    NSLock  * changeNotificationsLock;
    NSManagedObjectContext  * localManagedObjectContext;
    

    In your NSOperation main method before saving the context first apply all requested changes:

    [self.changeNotificationsLock lock];
    for(NSNotification * change in self.changeNotifications){
        [self.localManagedObjectContext mergeChangesFromContextDidSaveNotification:change];
    }
    if([self.changeNotifications count] >0){
        [self.changeNotifications removeAllObjects];
    }
    [self.changeNotificationsLock unlock];
    
    NSError *error = nil;   
    [self.localManagedObjectContext save:&error]
    

    Note that I used a lock, this because NSMutableArray is not thread safe and I want to safely access changeNotifications.
    changeNotifications is the array where all changes that need to be applied before saving the context are stored.

    And here is your merge method, modified so that all changes that need to be merged by your NSOperation are merged using the correct thread. Note that this methods is called by other threads than your NSOperation one, therefore you need to lock the access to self.changeNotifications

    - (void) mergeChanges:(NSNotification *)notification
     {
     // If not locally originated, then add notification into change notification array
     // this notification will be treated by the NSOperation thread when needed.
     if ([notification object] != self.localManagedObjectContext)
      {
         [self.changeNotificationsLock lock];
         [self.changeNotifications addObject:notification];
         [self.changeNotificationsLock unlock];
      }
    
     //Here you may want to trigger the main thread to update the main context     
    
    }
    

    Hope this help! This method is not 100% rock solid there may be some cases where a change notification may arrive too late. In that case the context save method will return an error and you have to reload the NSManagedObject and save it again.
    In case more details are needed please let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a book by O'Reilly that states that 70-80% of the performance
I have read through several reviews on Amazon and some books seem outdated. I
I have read a lot that LISP can redefine syntax on the fly, presumably
I have read about partial methods in the latest C# language specification , so
I have read that using database keys in a URL is a bad thing
I have read this post about how to test private methods. I usually do
I have read on Stack Overflow some people that have converting to C#2.0 to
I have read (or perhaps heard from a colleague) that in .NET, TransactionScope can
I have read the documentation on this and I think I understand. An AutoResetEvent
I have read the GOLD Homepage ( http://www.devincook.com/goldparser/ ) docs, FAQ and Wikipedia to

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.