I have a core data issue where some data is deleted somehow and my iPhone app will always crash on startup. I need a way to remove those data completely when this "CoreData could not fulfill a fault for" ex开发者_如何学Cception is detected.
Any advice or sample code on how to do this?
Is there a best practice or a right way to handle this exception?
This examples didn't crash.
-(void)removePreviousDestinationsFromMainDatabaseForCarrier:(NSString *)carrierName
{
NSManagedObjectContext *moc = [self managedObjectContext];
NSError *error = nil;
NSFetchRequest *requestCarrier = [[NSFetchRequest alloc] init];
[requestCarrier setEntity:[NSEntityDescription entityForName:@"Carrier" inManagedObjectContext:moc]];
[requestCarrier setPredicate:[NSPredicate predicateWithFormat:@"(name == %@)", carrierName]];
//[requestCarrier setIncludesPropertyValues:NO];
[requestCarrier setResultType:NSManagedObjectIDResultType];
//[requestCarrier setReturnsObjectsAsFaults:NO];
NSArray *carriers = [moc executeFetchRequest:requestCarrier error:&error];
if (error) NSLog(@"Failed to executeFetchRequest to data store: %@", [error localizedDescription]);
NSManagedObjectID *carrier = [carriers lastObject];
//NSManagedObject *carrier = [carriers lastObject];
[moc deleteObject:[moc objectWithID:carrier]];
//[managedObjectContext deleteObject:carrier];
[requestCarrier release], requestCarrier = nil;
return;
}
This is correct save calling. Do hide all interface issues which can thought u deleted object to avoid "collection was mutated while been enumerated"
[carriersTableView0 setHidden:YES];
[deleteProcess0 setHidden:NO];
[self removePreviousDestinationsFromMainDatabaseForCarrier:carrierName];
[moc save:&error];
[deleteProcess0 setHidden:YES];
[carriersTableView0 setHidden:NO];
I faced with the same symptoms (crash with message "CoreData could not fulfill a fault for") and tried lots of different variants (the most common answer is to check for thread safety but it didn't help me).
Finally, I've decided to try to replace simple types(float, int) I used for some number properties with the Objective C NSNumber and recreate the DB. And it solved my problem!
Of course it is not the only solution for this problem. I suppose, usually the problem is really connected with thread safety. But in some cases it might help.
Actually I suppose in my case the problem was caused by DB corrupting with using RestKit on changing DB table with simple types (instead of Objective C types).
精彩评论