Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I hade the same problem and here is how I solve it:</p> <p>Create a custom NSOperation Class where you define:</p> <pre><code>NSMutableArray * changeNotifications; NSLock * changeNotificationsLock; NSManagedObjectContext * localManagedObjectContext; </code></pre> <p>In your NSOperation main method before saving the context first apply all requested changes:</p> <pre><code>[self.changeNotificationsLock lock]; for(NSNotification * change in self.changeNotifications){ [self.localManagedObjectContext mergeChangesFromContextDidSaveNotification:change]; } if([self.changeNotifications count] &gt;0){ [self.changeNotifications removeAllObjects]; } [self.changeNotificationsLock unlock]; NSError *error = nil; [self.localManagedObjectContext save:&amp;error] </code></pre> <p>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.</p> <p>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</p> <pre><code>- (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 } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload