I have a following crash report from my released app:
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
continueWithExecutor:block:
means that thecontinuationBlock
runs in background thread. Task here is BFTask subclass, via link brief description. – Moccasin