Forcing Core Data to update NSManagedObject from a previous saved version when the updated version is already in memory

I am having trouble getting Core Data to update some of my NSManagedObject subclasses in a document-based iOS application I am writing.

The problem is to deal with a situation where the user downloads a second document while there are unsaved changes in the current document. At this point, if the user downloads the first document again, unsaved changes appear. Documents are loaded using NSFetchRequest on the document root. This fetch request has the property -setShouldRefreshRefetchedObjects:

set to YES

and the property is   -setIncludesPendingChanges:

set to NO

I am pretty sure they are not being saved to persistent storage as shutting down the application and restarting it results in the original unmodified document. Also, I have SQL debugging enabled and it shows that the application is not fetching from the respective tables a second time

+3


source to share


2 answers


This link can help you add a category to revert changes to NSManagedObject. Good luck.



#import "NSManagedObject+RevertChanges.h"

@implementation NSManagedObject (NSManagedObject_RevertChanges)

- (void) revertChanges {
    // Revert to original Values
    NSDictionary *changedValues = [self changedValues];
    NSDictionary *committedValues = [self committedValuesForKeys:[changedValues allKeys]];
    NSEnumerator *enumerator;
    id key;
    enumerator = [changedValues keyEnumerator];

    while ((key = [enumerator nextObject])) {
        NSLog(@"Reverting field ""%@"" from ""%@"" to ""%@""", key, [changedValues objectForKey:key], [committedValues objectForKey:key]);
        [self setValue:[committedValues objectForKey:key] forKey:key];
    }
}

      

0


source


I know this question has already been answered, but what about using the refreshObject: mergeChanges: method from NSManagedObjectContext?



see NSManagedObject Class reference refreshObject: mergeChanges:

+8


source







All Articles