Core Data moc reset causes fault on Lion, okay on Snow Leopard
I have an app that I developed on Snow Leopard with Xcode 3.x. It uses Core Data with a single entity called Parish.
It worked fine on Snow Leopard. When I upgraded my Macbook to Lion the app mostly worked okay, but now crashes in a specific case:
My app lets the user import data. I give the user the option to remove all data currently in the core data store before starting a new import.
I achieve removal of the data like this:
[self.managedObjectContext reset]; //to drop pending changes
[self.managedObjectContext lock];
// Now delete all peristent stores in this context
NSArray *stores = [self.managedObjectContext.persistentStoreCoordinator persistentStores];
for (NSPersistentStore *store in stores)
{
[self.managedObjectContext.persistentStoreCoordinator removePersistentStore:store error:nil];
[[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
}
[self.managedObjectContext unlock];
This worked fine on Snow Leopard, but running the same (unmodified) code on Lion results in this error when the [self.managedObjectContext reset]; call is made:
2011-08-29 17:37:24.349 ParishFinderHelperV2[2549:707] An uncaught exception was raised
2011-08-29 17:37:24.349 ParishFinderHelperV2[2549:707] CoreData could not fulfill a fault for
'0x107a0f9a0 <x-coredata://11995B9B-24CB-447F-BC18-E1A9F530C70C/Parish/p28367>'
2011-08-29 17:37:24.359 ParishFinderHelperV2[2549:707] (
0 CoreFoundation 0x00007fff963f6986 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8c60dd5e objc_exception_throw + 43
2 CoreData 0x00007fff97c24934 _PFFaultHandlerLookupRow + 916
3 CoreData 0x00007fff97c24254 _PF_FulfillDeferredFault + 212
4 CoreData 0x00007fff97c240d8 _sharedIMPL_pvfk_core + 56
5 CoreData 0x00007fff97c5ad3e -[NSManagedObject valueForKey:] + 222
6 Foundation 0x00007fff8d8ebe12 -[NSFunctionExpression expressionValueWithObject:context:] + 821
7 Foundation 0x00007fff8d8eba54 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 206
8 Foundation
I've looked through the Apple docs and Googled but can't see why this might be happening.
Could anyone suggest how I could fix this crash?
Update: One of the commenters (thanks) suggested deleting all records in the store manually. I have implemented this with the following code, which although works is incredibly slow given the large number of records I have to delete:
NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
[fetch setEntity:[NSEntityDescription entityForName:@"Parish" inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (id parish in result)
[context deleteObject:parish];
Could anyone suggest a way of deleting all the records (or 开发者_如何转开发the store itself) in one operation?
Thanks
Darren.
This seems to work well, and quickly:
NSPersistentStoreCoordinator *psc = [self.managedObjectContext.persistentStoreCoordinator retain];
NSArray *stores = [psc persistentStores];
// Delete the context and the psc
[self.managedObjectContext release], self.managedObjectContext = nil;
// Now delete all peristent stores in this context
for (NSPersistentStore *store in stores)
{
[psc removePersistentStore:store error:nil];
[[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
}
// Create the new stack now the old stack has been deleted
if (!(self.managedObjectContext = [[self createManagedObjectContext] retain]))
{
NSAssert(NO, @"Cannot create the managedObjectContext");
return (NO);
}
[psc release];
精彩评论