1
votes

I have an NSManagedObject (User) in database. Then I'm trying to fetch that object from database and update field firstName:

    NSFetchRequest *fetchR = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    NSError *err = nil;
    NSArray *allUsers = [self.managedObjectContext executeFetchRequest:fetchR error:&err];

    TMUser *profile = allUsers.firstObject;
    [profile setValue:@"Username" forKey:@"firstName"];

    [self.managedObjectContext save:&err];
    if (err) {
        NSLog(@"Error: %@", err.localizedDescription);
    }

The code passes without errors. But if I relaunch my app, fetch request retunrs user without updated field "firstName". I have only 1 NSManagedObjectContext. All Core Data stack was initialized successfully. After fetch my user is:

Printing description of allUsers:
<_PFArray 0x14ed6600>(
ID:3451
firstName:Johnatan
lastName:Hike
phone:380995046960
email:[email protected]
language:en
)

For some reason object changes wasn't registered in context(Context hasChanges = NO before save). What am I doing wrong? Please, help

2

2 Answers

0
votes

I think you are not saving the master context.

Please check that you call:

[managedObjectContext save:&error]; 

on all child contexts that save the data,

and after that on the master context as well.

You have one global function(in AppDelegate) saveContext which saves everything and which I can call from anywhere safely.

0
votes

I solved my problem. I recreated NSManagedObject subclass from xcdatamodeld scheme and it works. I found that if I add another properties(readonly etc.), not related to data model scheme or change property type from NSNumber(aka bool) to BOOL, it stops updating existed objects in database.