我的應用程序在設備上測試時崩潰,但在模擬器上未測試。它發生在我註銷時。我刪除從核心數據都記錄在用戶註銷時,代碼如下:從核心數據刪除對象 - 崩潰的應用程序
-(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;
}
任何幫助將不勝感激,我卡在這一個不是,也不知道從哪裏何去何從。
看着控制檯的輸出,也許崩潰報告肯定會有幫助。 – Eiko 2010-08-20 09:20:35
在調試器中查看應用程序崩潰的代碼行 – rano 2010-08-20 09:27:39
對不起,我應該從控制檯發佈結果。我現在就做。 – Stephen 2010-08-20 09:36:20