保存对托管对象上下文的更改时出现错误,但我的错误处理程序有问题:错误为零,因此没有给我任何有用的信息。我有两个版本的错误处理程序。这是由 Xcode 生成的,它可以工作(即,日志消息包含有用的错误信息):
AppDelegate.c
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
但我希望能够将成功/失败(最终,现在我只是中止)+错误信息传回给调用者,所以我有这个,它不起作用(错误为零,因此没有用有关错误的信息)。
数据库.h
+ (BOOL) commit:(NSError **)error;
数据库.c
+ (BOOL) commit:(NSError **)error {
AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
if (error == nil) {
NSLog(@"Unresolved error");
abort();
} else {
NSLog(@"Unresolved error %@, %@", *error, [*error userInfo]);
abort();
//return FALSE;
}
}
return TRUE;
}
return FALSE;
}
我很确定我的问题在于指针,并且迷失在重定向层中。
[编辑:]调用提交的代码:
[Database commit:nil];
我想知道是否需要在提交方法的开头添加类似的内容,但我不确定指针:
if (error == nil) {
error = [[NSError alloc] init];
}