【发布时间】:2010-08-20 09:17:33
【问题描述】:
我的应用在设备上测试时崩溃了,但在模拟器上却没有。当我退出时会发生这种情况。我在用户注销时删除core data中的所有记录,代码如下:
-(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]) 行上。
我不太确定为什么会发生这种情况,此时“锻炼”表中将没有记录,因此没有什么可删除的。模拟器似乎可以毫无问题地处理这个问题。
Persistent Store 代码详情:
- (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;
}
任何帮助将不胜感激,我被困在这个不知道从这里去哪里。
【问题讨论】:
-
看看控制台的输出,也许崩溃报告肯定会有帮助。
-
在调试器中查看应用程序崩溃的代码行
-
对不起,我也应该从控制台发布结果。我现在就做。
-
我已经用控制台和调试器的详细信息更新了我的帖子。
-
您的应用程序似乎没有有效的持久性存储,请发布您如何创建持久性存储的代码。