2011-08-29 47 views
1

我有一個應用程序,我開發雪豹與Xcode 3.x.它使用Core Data和一個名爲Parish的實體。核心數據moc重置導致獅子上的錯誤,好吧雪豹

它在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]; 

此工作得很好雪豹,但在這種錯誤運行上獅子結果相同(未修飾的)碼時,[self.managedObjectContext復位];通話是:

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       

我已經看過蘋果文檔和谷歌搜索,但看不出爲什麼會發生這種情況。

任何人都可以建議我如何解決這個崩潰?

更新: 其中一位評論者(謝謝)建議手動刪除商店中的所有記錄。我已經用下面的代碼,這雖然作品是非常緩慢的給予大量的記錄來實現這一點,我不得不刪除:

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]; 

可能有人建議刪除所有記錄的方式(或商店本身)的一個操作?

謝謝

Darren。

+0

我不認爲刪除持久存儲統籌,而依賴於它被管理對象上下文是開放的是有效的行爲。它可能適用於Snow Leopard,但我看不出它是如何有效的。你有什麼用途呢? –

+0

當用戶選擇將新數據集導入應用程序時,它們可以添加到當前記錄,也可以替換核心數據存儲區中的所有內容。當他們想要替換我執行上述代碼的所有內容時。這一切在SL上都可以正常工作,但在Lion上不會。你能否提出一個更好的方法來實現這個結果?謝謝,達倫。 –

+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]; 
相關問題