我有一个使用Xcode 3.x在Snow Leopard上开发的应用程序。它使用Core Data和一个名为Parish的实体。
它在Snow Leopard上运作良好。当我将Macbook升级到Lion时,应用程序大部分工作正常,但现在在特定情况下崩溃了:
我的应用可让用户导入数据。我为用户提供了在开始新导入之前删除核心数据存储中当前所有数据的选项。
我实现了删除这样的数据:
[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];
这在Snow Leopard上运行正常,但在Lion上运行相同(未修改)的代码会导致[self.managedObjectContext reset]时出现此错误;打电话:
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
我查看了Apple文档和谷歌搜索,但看不出为什么会发生这种情况。
有人可以建议我如何解决这个崩溃问题吗?
更新 其中一位评论者(谢谢)建议手动删除商店中的所有记录。我已经使用以下代码实现了这一点,尽管我必须删除大量记录,但是工作速度非常慢:
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];
有人可以建议在一次操作中删除所有记录(或商店本身)吗?
由于
达伦。
答案 0 :(得分:0)
这似乎运作良好,而且很快:
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];