【发布时间】:2010-01-11 16:49:53
【问题描述】:
我将一个已填充的现有 sqlite3 文件复制到我的项目中;找到了(我检查了
- (void)createEditableCopyOfDatabaseIfNeeded {
NSLog(@"Checking if we have to create editable copy of database");
// 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:@"hvw.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
NSLog(@"Creating editable copy of database");
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"hvw.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
我没有来到“创建数据库的可编辑副本”部分,所以文件在那里并找到了。
但是我的 fetch 没有返回任何结果:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Game" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
没有抛出错误,但结果数组为空...
编辑
我换了行
NSString *documentsDirectory = [paths objectAtIndex:0];
通过
NSString *documentsDirectory = [self applicationDocumentsDirectory];
在日志文件中我在尝试保存或读取对象时看到错误:
Operation could not be completed. (Cocoa error 256.)
【问题讨论】: