¿El mergeChangesFromContextDidSaveNotification de NSManagedObjectContext: publica 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:?

preguntado el 25 de abril de 13 a las 05:04

1 Respuestas

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

Perhaps your front UIViewController should observe NSManagedObjectContextObjectsDidChangeNotification.

Respondido 25 Abr '13, 05:04

Thanks rob, I tried that too and received the notification once out of like 500 times my context is changed. Do you know what would cause that? (I put a breakpoint when mergeChangesFromContextDidSaveNotification: is invoked and see it called every time) So weird. - rob5408

Actually it looks like I am not holding any references to any objects (only faults) and I think that's why I wasn't receiving the notification. As a test I chose one object from the store to hold onto and got the notification every time. Need to figure out the most logical way to set this up, but this has helped me, thanks! - rob5408

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.