在 coredata 中保存对象时出错
Posted
技术标签:
【中文标题】在 coredata 中保存对象时出错【英文标题】:Error while saving object in coredata 【发布时间】:2011-11-09 19:41:12 【问题描述】:这是我的代码:
- (IBAction) saveData
NSLog(@"saveData");
[self dismissKeyboard];
Fugitive *job = (Fugitive *)[NSEntityDescription insertNewObjectForEntityForName:@"Fugitive" inManagedObjectContext:managedObjectContext];
job.name = txtName.text;
NSError *error;
// here's where the actual save happens, and if it doesn't we print something out to the console
if (![managedObjectContext save:&error])
NSLog(@"Problem saving: %@", [error localizedDescription]);
// **** log objects currently in database ****
// create fetch object, this object fetch's the objects out of the database
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSURL *storeURL = [[NSBundle mainBundle] URLForResource:@"iBountyHunter" withExtension:@"momd"];
id globalStore = [[managedObjectContext persistentStoreCoordinator] persistentStoreForURL:storeURL];
[managedObjectContext assignObject:job toPersistentStore:globalStore];
for (NSManagedObject *info in fetchedObjects)
NSLog(@"Job Name: %@", [info valueForKey:@"name"]);
[fetchRequest release];
[self.navigationController dismissModalViewControllerAnimated:YES];
在控制台中我收到此错误:保存时出现问题:操作无法完成。 (可可错误 1570。) 新对象在 tableview 中创建,但未保存,因此在重新启动应用程序时,该对象消失
【问题讨论】:
您的核心数据堆栈中可能存在配置问题。在您设置模型、持久存储和上下文的位置发布代码。此外,如果您已将上下文配置为已有商店并且您的应用只有一个商店,则无需执行assignObject:toPersistentStore:
。
我已经把代码贴出来了,放在这里太长了
【参考方案1】:
- (void)saveContext
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
- (NSManagedObjectContext *)managedObjectContext
if (__managedObjectContext != nil)
return __managedObjectContext;
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
return __managedObjectContext;
- (NSManagedObjectModel *)managedObjectModel
if (__managedObjectModel != nil)
return __managedObjectModel;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"iBountyHunter" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
if (__persistentStoreCoordinator != nil)
return __persistentStoreCoordinator;
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"iBountyHunter.sqlite"];
NSError *error = nil;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
return __persistentStoreCoordinator;
【讨论】:
很高兴给出一些解释以及一大段旧代码! 这是解决方案,还是更多问题?以上是关于在 coredata 中保存对象时出错的主要内容,如果未能解决你的问题,请参考以下文章
数据导入CoreData期间如何显示progressBar?
在 Swift 中将 CoreData 保存/同步到 iCloud
将 UIImage 作为二进制数据保存到 CoreData 实体时出错