rob5408
rob5408

Reputation: 2982

Does NSManagedObjectContext's mergeChangesFromContextDidSaveNotification: post NSManagedObjectContextDidSaveNotification?

I have a bunch of NSOperations with their own NSManagedObjectContexts making changes to my Core Data store and saving and successfully getting their changes into the main thread's NSManagedObjectContext. This much I know. Now I want the front UIViewController to be notified when the main context is updated. So I...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshData:)
                                                 name:NSManagedObjectContextDidSaveNotification
                                               object:[NSManagedObject mainThreadManagedObjectContext]];
}

However I don't think after merging changes from background threads the main thread's NSManagedObjectContext is posting any notifications of its own. I tried to find somewhere in the docs where Apple says I should post my own after telling the main thread to merge changes or some such, but no luck. For extra reference in my NSOperation I have...

+ (void)mergeChanges:(NSNotification *)notification
{
    NSManagedObjectContext *managedObjectContext = [self mainThreadManagedObjectContext];

    [managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                                           withObject:notification
                                        waitUntilDone:YES];
}

+ (NSManagedObjectContext *)adHocManagedObjectContext
{
    NSManagedObjectContext *adHocManagedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] adHocManagedObjectContext];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(mergeChanges:)
                                                 name:NSManagedObjectContextDidSaveNotification
                                               object:adHocManagedObjectContext];

    return adHocManagedObjectContext;
}

Should I expect to hear from the main thread's NSManagedObjectContext about it saving or should I post my own notification after mergeChangesFromContextDidSaveNotification:?

Upvotes: 1

Views: 2270

Answers (1)

rob mayoff
rob mayoff

Reputation: 385950

mergeChangesFromContextDidSaveNotification: does not post NSManagedObjectContextDidSaveNotification, because mergeChangesFromContextDidSaveNotification: does not tell the context to save.

Perhaps your front UIViewController should observe NSManagedObjectContextObjectsDidChangeNotification.

Upvotes: 5

Related Questions