1
votes

Core Data and multithreading:

  1. Save in one context
  2. Wait for completion
  3. Grab the object id and request it from other context.

I'm getting error 133000 and the object on the background thread is NIL. It seems like the save was not completed. why?

    __block ChapterData *ch1;
     [MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {
        ch1 = [ChapterData MR_createInContext:localContext];
        ch1.name = @"My Chapter";
    } completion:^{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error;
            NSManagedObject *ch2ByObjectId = [[NSManagedObjectContext MR_contextForCurrentThread] existingObjectWithID:ch1.objectID error:&error];
            GHAssertNotNil(ch2ByObjectId, @"Ch2 by Ch1 object id is NIL");
        });

    }];

To my understanding this should work, yet it fails the assertion.

1

1 Answers

0
votes

I am not sure if you are still having this problem, but here goes my late reply: You aren't actually invoking a save method on a ManagedObjectContext. You are only asking the object to be created in your "localContext", but no save is performed.