0
這裏是我的代碼:錯誤而coredata節省對象
- (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:'。 – XJones
我已經發布了代碼作爲答案,它太長了把它放在這裏 –