coredata导入sqlite但没有保存新数据Cocoa错误256

时间:2011-02-16 23:30:37

标签: iphone sqlite core-data

编辑,//没有节省的原因>

所以我在保存已编辑和保存的新数据后,在我的saveData中添加了一些nslog(请在下面的代码中检查) 对于编辑,很好 但对于新的,我得到>

Error The operation couldn’t be completed. (Cocoa error 256.)

**第二次编辑> 如果我第一次编辑和保存,它会保存编辑后的数据,但对新数据显示错误256,

但是如果我在出现256错误后尝试保存编辑后的数据(通过尝试保存新数据),那么编辑后的数据会在保存时显示错误256并且不会保存!!!!

我导入了一些sqlitedb(与coredata生成的相同,但有一些预先填充的表)

- (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_;
}

它创建了新的数据库,并使用现有数据填充它,当我在应用程序中编辑数据(预填充)时,它保存这些数据(在sqlite管理器的模拟器文档中检查,然后再出去),但如果我创建一个新条目,它会在显示数据的表格中显示,但是当我离开应用程序,然后返回时,这个新创建的条目不存在(当然不在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 个答案:

答案 0 :(得分:0)

要尝试的几件事: 1.在保存之前,请确保managedObjectContext具有非null值。 2.您的db初始化代码是否正在重新安装原始数据库(从而丢失您的更改)? 祝你好运。