2010-08-20 141 views
0

我的應用程序在設備上測試時崩潰,但在模擬器上未測試。它發生在我註銷時。我刪除從核心數據都記錄在用戶註銷時,代碼如下:從核心數據刪除對象 - 崩潰的應用程序

-(IBAction)logOut 
{ 
    UIAlertView *getConfirmation = [[UIAlertView alloc] initWithTitle:@"Confirm" message:@"Are you sure you want to logout. You will lose any unsync'ed workouts." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Logout", nil]; 
    [getConfirmation show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1){ 
     // Clear the database of all objects when the user logs out. 
     [self deleteAllObjects:@"Workout"]; 
     [self deleteAllObjects:@"Route"]; 
     [self deleteAllObjects:@"WayPoint"]; 
     [self deleteAllObjects:@"Graphs"]; 
     [self deleteAllObjects:@"userSettings"]; 

     [self presentModalViewController:loginViewController animated:NO]; 
    } 
} 


-(void)deleteAllObjects:(NSString *)entityDescription{ 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSError *error; 
    NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    [fetchRequest release]; 

    for (NSManagedObject *managedObject in items) { 
     [managedObjectContext deleteObject:managedObject]; 
     NSLog(@"%@ object deleted", entityDescription); 
    } 

    if (![managedObjectContext save:&error]) { 
     NSLog(@"Error deleting %@ - error:%@",entityDescription,error); 
    } 
} 

這似乎是發生在我登錄和註銷立竿見影。在這種情況下,5個表中的4個不會有對象(注意:userSettings會有1條記錄)。

看看控制檯的錯誤消息是'***終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException',原因:'此NSPersistentStoreCoordinator沒有持久性存儲。它不能執行保存操作。'',看着調試器發生在線if (![managedObjectContext save:&error])上。

我不太清楚爲什麼會發生這種情況,此時在表格「鍛鍊」中沒有記錄,因此沒有任何可刪除的記錄。模擬器似乎處理這個沒有問題。持久性存儲代碼的

詳情:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"LegginitCoreData.sqlite"]]; 

    NSError *error; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     // Handle error 
    }  

    return persistentStoreCoordinator; 
} 

任何幫助將不勝感激,我卡在這一個不是,也不知道從哪裏何去何從。

+0

看着控制檯的輸出,也許崩潰報告肯定會有幫助。 – Eiko 2010-08-20 09:20:35

+0

在調試器中查看應用程序崩潰的代碼行 – rano 2010-08-20 09:27:39

+0

對不起,我應該從控制檯發佈結果。我現在就做。 – Stephen 2010-08-20 09:36:20

回答

1

正如Martin所說,您的錯誤似乎集中在NSPersistentStore問題上。處理這個錯誤,而不是有// Handle error的評論將是一個非常好的第一步。你很可能會在那裏發現一個錯誤並忽略它。

此外,在退出時,如果您的目標是刪除所有數據,則有一種更簡單的方法。

撕下核心數據堆棧(釋放NSManagedObjectContext,NSPersistentStoreCoordinatorNSManagedObjectModel),然後刪除SQLite文件。在下次啓動時,所有數據都消失了。