在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”

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...