使用核心数据将数据存储到 sqlite 表的问题
Posted
技术标签:
【中文标题】使用核心数据将数据存储到 sqlite 表的问题【英文标题】:problem storing data to sqlite table using core data 【发布时间】:2011-06-25 03:51:12 【问题描述】: 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】:您需要在viewWillDisappear:
方法的末尾保存上下文以实际提交您的更改
NSError *error = nil;
if (![context save: &error])
// Couldn't save
【讨论】:
它完成了。但很抱歉,我不知道这段代码是如何提交插入的。你能解释一下吗?context
是文档中提到的便签本。您的所有更改(插入、删除和编辑)都存储在上下文中,并且只有在您调用 save:
时才会写入数据库。如果你不这样做,数据库就不会改变。以上是关于使用核心数据将数据存储到 sqlite 表的问题的主要内容,如果未能解决你的问题,请参考以下文章