Does core data refresh data model with a delay?

868 Views Asked by At

I faced with a problem that the core data don't see any changes in my predicate when I'm tryin to modify it. I spent alot of time trying to find why doesn't my fetched property show me the correct results (see code below) and finally after another coffee break I've seen that (SUDDENLY!) my fetched property starts to work fine. Is there some delay with updating datamodel? Or should I go on breaks more often?

-(void)printData{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Empl"];
NSError *error;
NSArray *empls = [[self managedObjectContext] executeFetchRequest:request error:&error];

for (NSManagedObject *emplMO in empls){

    NSString *output = [NSString stringWithFormat:@"%@ \n", [emplMO valueForKey:@"emplName"]];
    [self.managedObjectContext refreshObject:emplMO mergeChanges:YES];
    NSArray *allChairs = [emplMO valueForKey:@"allChairs"];
    output = [output stringByAppendingFormat:@" has %d chairs \n", allChairs.count];
    NSLog(@"%@", output);
}
}

Here is my model.

1

There are 1 best solutions below

1
On

Are you using single NSManagedObjectContext across your whole app?

from Apple Developer Reference:

An instance of NSManagedObjectContext represents a single “object space” or scratch pad in an application....Through a context you can retrieve or “fetch” objects from a persistent store, make changes to those objects, and then either discard the changes or—again through the context—commit them back to the persistent store. The context is responsible for watching for changes in its objects and maintains an undo manager so you can have finer-grained control over undo and redo.

Sounds like if you have two contexts, the objects changed in contextA will need to be persisted out to store and back out again for the contextB to see them... UIManagedDocument does autosaving. You may see slight delay/gap in core data update/fetch if you are using 2 or more contexts.