Search code examples
iosobjective-ccore-datamagicalrecordaws-sdk-ios

Core Data concurrency `performBlockAndWait:` NSManagedObjectContext zombie


I have a following crash report from my released app:

enter image description here

synchronizeMyWords method fetches the entities from database, creates private queue context with main context parent and finally saves results. All operations are in the background thread. This method being called every time app goes into background and foreground. Here is a simplified method:

- (AWSTask *)synchronizeMyWords {
  __weak typeof(self) weakSelf = self;

  AWSContinuationBlock block = ^id _Nullable(AWSTask * _Nonnull task) {
    if ([task.result isKindOfClass:[NSArray class]]) {
      NSArray * records = (NSArray *)task.result;
      NSManagedObjectContext * context = [NSManagedObjectContext MR_contextWithParent:[NSManagedObjectContext MR_defaultContext]];
      [context performBlockAndWait:^{
        for (NSDictionary * info in records) {
            [RDRWord MR_createEntityInContext:context];
        }

        [context save:nil];
      }];
      return [AWSTask taskWithResult:@YES];
    }
    return [AWSTask taskWithError:[NSError errorWithDomain:@"" code:404 userInfo:nil]];
  };

  AWSExecutor * executor = [AWSExecutor defaultExecutor];


  return [[self loadLocalWords] continueWithExecutor:executor withBlock:block];
}

As you see I am using Magical Record 3rd party library to manage Core Data stack. Here is a method of creating private queue context:

+ (NSManagedObjectContext *) MR_contextWithParent:(NSManagedObjectContext *)parentContext
{
    NSManagedObjectContext *context = [self MR_newPrivateQueueContext];
    [context setParentContext:parentContext];
    [context MR_obtainPermanentIDsBeforeSaving];
    return context;
}

You can check the whole NSManagedObjectContext+MagicalRecord category on github here.

How is it available that context object inside performBlockAndWait: released before it escapes the scope? I am personally not able to reproduce the crash, but a lot of my users (iOS 8.1 - 10 devices) are affected by this issue.

UPDATE 1:

Here is for instance same report on blog


Solution

  • Core Data provides ample APIs to deal with background threads. These are also accessible via Magical Record.

    It looks as if you creating too many threads unnecessarily. I think that the employment of AWSContinuationBlock and AWSExecutor is not a good idea. synchronizeMyWords could be called from a background thread. The block might be run on a background thread. Inside the block you create a new background thread linked to the child context. It is not clear what loadLocalWords returns, or how continueWithExecutor:block: deals with threads.

    There is also a problem with the saving of the data. The main context is not saved after the child context is saved; presumably this happens later, but perhaps in connection with some other operation, so that the fact that your code was working before is perhaps more of a "false positive".

    My recommendation is to simplify the threading code. You should confine yourself to the Core Data block APIs.