Search code examples
iosobjective-ccore-datansmanagedobjectcontext

request to core data after changes in it gives the old value


i have this code into core data model for update a value into it:

if (!([photo.photoDescription isEqualToString:[photoDictionary[PHOTO_DESCRIPTION] description]])) {
        photo.photoDescription = [photoDictionary[PHOTO_DESCRIPTION] description];
        photo.isOnMap = [NSNumber numberWithBool:NO];
        [context save:&error];
    }

in my controller i have add an observer for looking the changes into core data:

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificaRicevut) name:NSManagedObjectContextDidSaveNotification object:self.context];

when i execute the [context save:&error]; code, the notify is correctly sent, but when i make a request inside the notification method for retrieve the new value, i get the old value instead the new one. if i restart the app, the new value is show correctly. where is the problem? thanks


Solution

  • i solved by putting this code inside the notification method:

        - (void)contextDidSave:(NSNotification *)notification
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
            [self.context performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES];
            [self updateMapWithPictureInCoreData];
        });
    }
    

    Hope can be helpful