0

I saved my managedObjects. But my NSManagedObjectID is still temporary after saving. Why?

dispatch_async(privateQueue, ^{
    __block NSMutableArray *ids = [NSMutableArray array];

    [[[LPAppDelegate instance] privateContext] performBlockAndWait:^{

        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            id arr = ((NSDictionary*)responseObject)[@"results"];
            for (int i=0; i < ((NSArray *)arr).count; i++) {
                LPFilm *film = [LPFilm MR_createEntityInContext:privateContext];
                [ids addObject:film.objectID];
            }
        }

        NSError *error = nil;
        [privateContext save:&error];
        if (error) {
            NSLog(@"___fetch service error: %@", [error localizedDescription]);
        }
    }];


    for (NSManagedObjectID *objID in ids) {
        if (objID.isTemporaryID) {
            NSLog(@"__tempID %@", objID);
        }
    }
});
Voloda2
  • 12,359
  • 18
  • 80
  • 130

1 Answers1

1

When you save changes, new NSManagedObject instances get a new object ID. Previously existing instances of NSManagedObjectID are not converted in-place, they're replaced with new instances. But you have to ask the managed object for its new ID.

In your case, you're saving up an array of object IDs before saving. Those objects will never change, even if you save changes. But, if you go back to your managed objects and ask them for their object IDs again, you'll get different results, which will not be temporary.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170