Iphone coredata导入sqlite但不保存新数据错误256
编辑,//不保存的原因> 所以我在我的saveData中放了一些nslog(请在下面的代码中签入),在保存编辑后,保存了新数据, 对于编辑过的文档,这很好 但对于新的,我得到>Iphone coredata导入sqlite但不保存新数据错误256,iphone,sqlite,core-data,Iphone,Sqlite,Core Data,编辑,//不保存的原因> 所以我在我的saveData中放了一些nslog(请在下面的代码中签入),在保存编辑后,保存了新数据, 对于编辑过的文档,这很好 但对于新的,我得到> Error The operation couldn’t be completed. (Cocoa error 256.) **第二次编辑> 如果我第一次编辑并保存,它将精细保存编辑的数据,但新数据显示错误256 但是如果我在出现256错误后尝试保存编辑的数据(通过尝试保存新数据),则编辑的数据在保存时显示错误256,
Error The operation couldn’t be completed. (Cocoa error 256.)
**第二次编辑>
如果我第一次编辑并保存,它将精细保存编辑的数据,但新数据显示错误256
但是如果我在出现256错误后尝试保存编辑的数据(通过尝试保存新数据),则编辑的数据在保存时显示错误256,并且不会保存
我正在导入一些sqlitedb(与coredata生成的相同,但带有一些预填充的表)
它创建新的db fine,并用现有数据填充它,当我编辑数据(预填充)时,在应用程序中,它保存此数据(使用sqlite manager签入模拟器文档,然后进出),但如果我创建一个新条目,它会显示在显示数据的表中,但当我离开应用程序时,回到这里,这个新创建的条目不在那里(当然也不在sqlite新数据库中)
这是我用来保存编辑数据或新创建数据的代码
-(IBAction)saveData
{
if(editFlag==1)
{
NSManagedObjectContext *context = [(ChildCare_v01AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context];
[request setEntity:entity];
editEmp.namemployee = tfName.text;
editEmp.surname = tfSurname.text;
editEmp.email = tfEmail.text;
//editEmp.phone = tfPhone.text;
editEmp.mobile = tfMobile.text;
editEmp.category=tfCategory.text;
NSLog(@"salvado editado"); //saving FINE!!!!
NSError *error;
if (![context save:&error])
{ NSLog(@"Error %@",[error localizedDescription]); }
//[context release];
[self.navigationController popViewControllerAnimated:TRUE];
}
else{
NSManagedObjectContext *context = [(ChildCare_v01AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
Employee *employee = (Employee*)[NSEntityDescription
insertNewObjectForEntityForName:@"Employee"
inManagedObjectContext:context];
employee.namemployee = tfName.text;
employee.surname = tfSurname.text;
employee.email = tfEmail.text;
//employee.phone=tfPhone.text;
employee.mobile = tfMobile.text;
employee.category=tfCategory.text;
NSLog(@"salvado nuevo"); ///HERE THE ERROR!! COCOA ERROR 256!!!!
NSError *error;
if (![context save:&error]) {
NSLog(@"Error %@",[error localizedDescription]);
// handle error
} else {
NSLog(@"Done");
}
[self.navigationController popViewControllerAnimated:TRUE];
}
}
- 那么是否可以保存新创建的数据??为什么不工作?为什么只保存编辑?没有新数据吗?请帮我实现这一点,非常感谢李>
- 有几件事可以尝试:
1.在保存到managedObjectContext之前,请确保其具有非空值。
2.数据库初始化代码是否重新安装了原始数据库(从而丢失了更改)?
祝您好运。您好,谢谢您的回复,我必须检查数字1,检查时将进行编辑,至于2,原始数据库不会重新安装,因为我在编辑时所做的更改已保存(使用sqlite管理器对此进行了检查,在原始文件夹中,原始数据保持不变,但在模拟器文件夹中,sqlite保存该编辑的数据)第1条,“便笺簿不为空”我可以在点击save后看到表中的数据……但是当离开应用程序时,数据不再存在;在persistenStoreCoordinator方法中,不是每次都调用此代码吗?这不会覆盖db吗?if(defaultStorePath){[fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath错误:NULL];NSLog(@“storePath=%@”,storePath);}}}嗨,tnx,我不明白db是如何被覆盖的,因为如果我编辑加载的数据(从sqlite预填充文件),这些编辑后的数据保存在coredata sqlite中(在模拟器和手机中),在sqlite管理器中签入并退出,在应用程序中,是否存在,,,但如果我创建了一个新条目,则不会保存此新条目..好的,我看到覆盖代码在if块中。对此表示抱歉。您如何知道插入失败?您是否看到错误消息,或者只是结果中没有该消息?您的查询是什么确定吗?执行fetchRequest之前是否清除缓存?出现错误时转储错误的用户信息。
这将为您提供有关错误原因的更多信息。NSLog(@“error%@[%@],[error localizedDescription],[error userInfo]);
-(IBAction)saveData { if(editFlag==1) { NSManagedObjectContext *context = [(ChildCare_v01AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context]; [request setEntity:entity]; editEmp.namemployee = tfName.text; editEmp.surname = tfSurname.text; editEmp.email = tfEmail.text; //editEmp.phone = tfPhone.text; editEmp.mobile = tfMobile.text; editEmp.category=tfCategory.text; NSLog(@"salvado editado"); //saving FINE!!!! NSError *error; if (![context save:&error]) { NSLog(@"Error %@",[error localizedDescription]); } //[context release]; [self.navigationController popViewControllerAnimated:TRUE]; } else{ NSManagedObjectContext *context = [(ChildCare_v01AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; Employee *employee = (Employee*)[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:context]; employee.namemployee = tfName.text; employee.surname = tfSurname.text; employee.email = tfEmail.text; //employee.phone=tfPhone.text; employee.mobile = tfMobile.text; employee.category=tfCategory.text; NSLog(@"salvado nuevo"); ///HERE THE ERROR!! COCOA ERROR 256!!!! NSError *error; if (![context save:&error]) { NSLog(@"Error %@",[error localizedDescription]); // handle error } else { NSLog(@"Done"); } [self.navigationController popViewControllerAnimated:TRUE]; } }