在iOS 7中预加载数据库

在过去,我已经使用预加载数据库发布了我的应用程序,因此用户无需在首次运行时更新它.我在另一个关于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
}

当我尝试在iOS 7中执行此操作时,我没有收到任何错误,但数据库是空的(即使我的mainBundle中的数据库具有我期望的所有信息).我注意到applicationDocumentsDirectory中有更多的数据库文件(.sqlite-shm文件和.sqlite-wal文件).我还需要对这些文件做些什么吗?或者是否已经不再可以将预装数据库与应用程序一起发货?

编辑:我尝试添加代码来复制新的.sqlite-shm和.sqlite-wal文件,但这没有帮助.

解决方法

核心数据在iOS 7中有所改变,主要是如何执行保存.

提前写入日志(wal)是为了提高性能,因此你看到了WAL sqlite文件.

您可以告诉您的应用使用旧的“日记帐模式”:

You can specify the journal mode by adding the NSsqlitePragmasOption
to the options when calling
addPersistentStoreWithType:configuration:url:options:error. E.g. to
set the prevIoUs default mode of DELETE:

NSDictionary *options = @{ NSsqlitePragmasOption : @{@"journal_mode" : @"DELETE"} };

Source

我不确定这是否能解决您的问题,但是iOS 7中的核心数据已经发生了变化

如果你想了解更多关于WAL的信息,我建议你观看WWDC session #207,“Whats New In Core Data & iCloud”

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...