使用核心数据将数据存储到sqlite表的问题

时间:2011-06-25 03:51:12

标签: objective-c cocoa-touch ios4 core-data

    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数据但未保存到表中。

1 个答案:

答案 0 :(得分:0)

您需要在viewWillDisappear:方法结束时保存上下文以实际提交更改

NSError *error = nil; 
if (![context save: &error]) {
    // Couldn't save
}