Gen*_*ike 6 core-data objective-c ios ios7
在过去,我已经使用预加载的数据库发布了我的应用程序,因此用户无需在首次运行时更新它.我在另一个关于SO的问题中发现了一些代码(抱歉,我没有链接)我添加到我的App Delegate的persistentStoreCoordinator方法中:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"db.sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]])
{
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite"]];
NSError* err = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err])
{
NSLog (@"Error - Could not preload database.");
}
}
//... more code generated from the template here
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在iOS 7中执行此操作时,我没有收到任何错误,但数据库是空的(即使我的数据库中mainBundle包含我期望的所有信息).我注意到有更多的数据库文件(.sqlite-shm文件和.sqlite-wal文件)applicationDocumentsDirectory.我还需要对这些文件做些什么吗?或者是否已经不再可以将预装数据库与应用程序一起发货?
编辑:我尝试添加代码来复制新的.sqlite-shm和.sqlite-wal文件,但这没有帮助.
核心数据在iOS 7中有所改变,主要是如何执行保存.
提前写入日志(wal)是为了提高性能,因此你看到了WAL sqlite文件.
您可以告诉您的应用使用旧的"日记帐模式":
您可以通过在调用addPersistentStoreWithType:configuration:url:options:error时将NSSQLitePragmasOption添加到选项来指定日志模式.例如,设置以前的DELETE默认模式:
NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };
Run Code Online (Sandbox Code Playgroud)
我不确定这是否能解决您的问题,但是iOS 7中的核心数据已经发生了变化
如果你想了解更多有关WAL的信息,我建议你观看WWDC会议#207,"核心数据和iCloud的新内容"
我也复制了.sqlite-shm和.sqlite-wal文件并且它有效:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *storePath = [documentsDirectory stringByAppendingPathComponent: @"emm_samples.sqlite"];
// Check if the sqlite store exists
if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) {
NSLog(@"File not found... copy from bundle");
// copy the sqlite files to the store location.
NSString *bundleStore = [[NSBundle mainBundle] pathForResource:@"emm_samples" ofType:@"sqlite"];
[[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:nil];
bundleStore = [[NSBundle mainBundle] pathForResource:@"emm_samples" ofType:@"sqlite-wal"];
storePath = [documentsDirectory stringByAppendingPathComponent: @"emm_samples.sqlite-wal"];
[[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:nil];
bundleStore = [[NSBundle mainBundle] pathForResource:@"emm_samples" ofType:@"sqlite-shm"];
storePath = [documentsDirectory stringByAppendingPathComponent: @"emm_samples.sqlite-shm"];
[[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:nil];
}
else {
NSLog(@"File exists");
}
Run Code Online (Sandbox Code Playgroud)
(基于:App Bundle中包含的Core Data Store)
| 归档时间: |
|
| 查看次数: |
3109 次 |
| 最近记录: |