We are running into many concurrency related crashes related to core data and deletes with our app, so I created a small project to reproduce one of those scenario. I am able to reproduce a crash with "CoreData could not fulfill a fault" in the following scenario: - I have 2 child contexts A, B, both associated with the same main parent content. - the coredata model is very simple, one ConferenceRoom object has many Line objects. - context A and B have concurrency type "NSPrivateQueueConcurrencyType", parent with type "NSMainQueueConcurrencyType" - thread 1 fetches an object from child context A, faults it, deletes it in context A and the main parent context - thread 2 fetches the same object from child context B, waits for 2 seconds, saves it in the context B and the main parent context.
-->the app crashes in thread2 with "CoreData could not fulfill a fault" when it tries to save to child context B
Note that we've already fixed issues after trying the new xcode6 coredata debug flag:"-com.apple.CoreData.ConcurrencyDebug 1 ", so no threading warning/issues in theory... So can anyone explain how we can avoid those crashes? (I can send the full project if needed).
Here is the code (I am a novice ios developer, and it's quick/dirty code for sure)
Core methods:
//this creates a Conference and a Line object (called from AppDelegate when app launches
- (void) testCrash
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM HH:mm:ss"];
NSString *initConfName = [formatter stringFromDate:[NSDate date]];
//fix
[self.managedObjectChildContext performBlockAndWait:^{
ConferenceRoom *room = [NSEntityDescription insertNewObjectForEntityForName:@"ConferenceRoom" inManagedObjectContext:self.managedObjectChildContext];
room.name = initConfName;
Line *line1 = [NSEntityDescription insertNewObjectForEntityForName:@"Line" inManagedObjectContext:self.managedObjectChildContext];
line1.phoneNumber = @"4154243243";
NSMutableSet *lines = [room mutableSetValueForKey:@"lines"];
[lines addObject:line1];
}];
[self saveChildContext];
[self saveContext];
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(mainThread1:)
object:initConfName];
[myThread start];
NSThread* myThread2 = [[NSThread alloc] initWithTarget:self
selector:@selector(mainThread2:)
object:initConfName];
[myThread2 start];
}
- (void) mainThread1:(NSString *) initConfName
{
NSLog(@"started thread 1");
//GET OBJ FROM CHILD CONTEXT 1
[self.managedObjectChildContext performBlockAndWait:^{
NSArray *results = [self getConfRoom: self.managedObjectChildContext withName:initConfName];
NSLog(@"C1 conf:%@", results);
ConferenceRoom *roomFoundChild1 = [results lastObject];
NSArray *linesc1 = [[roomFoundChild1 mutableSetValueForKey:@"lines"] allObjects];
Line *linec1 = [linesc1 firstObject];
NSLog(@"LINEC1=%@", linec1);
//DELETE CONF IN CHILD CONTEXT 1:
NSLog(@"Thread1:going to delete conference %@", roomFoundChild1);
[self.managedObjectChildContext deleteObject: roomFoundChild1];
}];
NSLog(@"Thread1: before saving child context");
[self saveThisContext: self.managedObjectChildContext];
NSLog(@"Thread1: before saving main context");
//test: save in main context, works without this
[self saveContext];
}
- (void) mainThread2:(NSString*) initConfName
{
NSLog(@"started thread 2");
//GET OBJ FROM CHILD CONTEXT 2
__block NSArray *results;
__block ConferenceRoom *roomFoundChild2;
__block NSString *newName;
[self.managedObjectChildTwoContext performBlockAndWait:^{
results = [self getConfRoom: self.managedObjectChildTwoContext withName:initConfName];
NSLog(@"C2 conf\n:%@", results);
roomFoundChild2 = [results lastObject];
NSString *n = roomFoundChild2.name;
//UPDATE CONF ROOM IN CHILD CONTEXT 2
newName = [NSString stringWithFormat:@"%@-%@", initConfName, @"newName2"];
NSLog(@"Thread 2 waiting");
[NSThread sleepForTimeInterval:2];
NSLog(@"Thread 2 resuming");
roomFoundChild2.name = newName;
NSLog(@"roomFoundChild2, %@", roomFoundChild2);
}];
NSLog(@"Thread2: before saving child context");
[self saveThisContext:self.managedObjectChildTwoContext];
NSLog(@"Thread2: after saving to child context");
results = [self getConfRoom:self.managedObjectChildTwoContext withName:newName];
NSLog(@"C2 context after delete:%@", results);
NSLog(@"Thread2: before saving main context");
//test: save in main context, works without this
[self saveContext];
}
- (void)saveContext
{
// NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
[managedObjectContext performBlockAndWait:^{
if ([managedObjectContext hasChanges]) {
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved ERROR %@, %@", error, [error userInfo]);
abort();
};
}
}];
}
}
- (void)saveChildContext
{
// NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectChildContext;
if (managedObjectContext != nil) {
//COREDATAFLAG CHANGE
[managedObjectContext performBlockAndWait:^{
if ([managedObjectContext hasChanges]) {
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved ERROR %@, %@", error, [error userInfo]);
abort();
};
}
}];
}
}
- (void) saveThisContext: (NSManagedObjectContext *)ctx
{
if (ctx != nil) {
[ctx performBlockAndWait:^{
if ([ctx hasChanges]) {
NSError *error = nil;
if (![ctx save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved ERROR %@, %@", error, [error userInfo]);
//removed abort to test..
//abort();
};
}
}];
}
}