1

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.

4

1 に答える 1