EDIT, // REASON FOR NOT SAVING>
so I put some nslog in my saveData (plese check in below code), after saving edited, and saved new data, for the edited, is fine but for the new,, I get>
Error The operation couldn’t be completed. (Cocoa error 256.)
** Second Edit> if I edit and save the first time, it saves fine the edited data, but shows error 256 for new data,
but if I try to save the edited data after having the 256 error (by trying to save new data), then the edited data shows error 256 when saving and doesnt get saved!!!!
Im importing some sqlitedb (the same that coredata generates but with some prepopulated tables)
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"ChildCare_v02.sqlite"];
NSString *storePath = [[NSBundle mainBundle] pathForResource:@"ChildCare_v02" ofType:@"sqlite"];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ChildCare_v02.sqlite"]; //este es el que sirve!!! CREE ESTE
NSLog(@"store URL %@", storeURL);
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:writableDBPath]) {
NSLog(@"proceda aqui");
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"ChildCare_v02" ofType:@"sqlite"];
NSLog(@"no existe todavia");
NSLog(@"defalultStorePath %@", defaultStorePath);
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath error:NULL];
NSLog(@"storePath= %@", storePath);
}
}
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
it creates the new db fine, and populates it with the existing data, when I edit the data (prepopulated), in the app, it saves this data (checked in simulator documen开发者_开发知识库t with sqlite manager, and going out and in again), but if I create a new entry, it shows in the table showing the data, but when I leave the app, and come back then this newly created entry is not there (and of course not in the sqlite new db)...
this is the code I use for saving edited data or newly created data,
-(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];
}
}
- so its it possible to save the newly created data?? why is not working? why only saves edits? and no new data?, please help me to achieve this, thanks a lot!
A couple things to try: 1. Make sure the managedObjectContext has a non-null value before you save to it. 2. Is your db initialization code re-installing the original db (thereby losing your changes)? Good luck.
精彩评论