使用核心数据将数据存储到sqlite表时出现问题

|
    IfIDieAppDelegate.m

   - (void)createEditableCopyOfDatabaseIfNeeded

    { 

    // First, test for existence. 
    BOOL success; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSError *error; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    //NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@\"DeathDiary.sqlite\"];

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@\"IfIDie.sqlite\"];

    NSLog(@\"%@\",writableDBPath);

    success = [fileManager fileExistsAtPath:writableDBPath]; 

    if (success==YES)

    {

        NSLog(@\"Database Already Exists\");
        return;
    }
    else {

        NSLog(@\"New Database Created\");

        // The writable database does not exist, so copy the default to the appropriate location. 


    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@\"IfIDie.sqlite\"]; 

        NSLog(@\"Default : %@\",defaultDBPath);

        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 

        if (!success) { 

            NSAssert1(0, @\"Failed to create writable database file with message \'%@\'.\", [error localizedDescription]); 

        } 

    }

}


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {

        return persistentStoreCoordinator;

    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @\"IfIDie.sqlite\"]];

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


NoteEditController.m


- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

    if(saveChanges){

        // Save any changes to note
        if([[noteTextView text] length] > 0){

            if(noteToEdit){

                // Edit Note
                if([[titleField text] length] <= 0)

                    [noteToEdit setNoteTitle:[noteTextView text]];

                else

                    [noteToEdit setNoteTitle:[titleField text]];

                [noteToEdit setNoteText:[noteTextView text]];

            } else {
                // New Note
                Note *newNote = [NSEntityDescription 
insertNewObjectForEntityForName:@\"Note\" inManagedObjectContext:context];

                if([[titleField text] length] <= 0)

                    [newNote setNoteTitle:[noteTextView text]];

                else

                    [newNote setNoteTitle:[titleField text]];


                [newNote setNoteText:[noteTextView text]];

                NSLog(@\"data saved\");


            }

        } else {
            // Remove note (zero length)

            if(noteToEdit){

                [context deleteObject:noteToEdit];
            }
        }
    }
}
一切似乎都正常,但仍然没有将数据保存到表中。可能是什么错误? 是否有一些数据库重载?如果,那么我不知道如何解决。 那里没有错误。它显示已保存但未保存到表的数据的nslog。     
已邀请:
        您将需要保存上下文以在
viewWillDisappear:
方法末尾实际提交更改。
NSError *error = nil; 
if (![context save: &error]) {
    // Couldn\'t save
}
    

要回复问题请先登录注册