过去,我使用预先加载的数据库发布了我的应用程序,因此用户不必在第一次运行时对其进行更新。我在SO Delegate的persistentStoreCoordinator方法中添加了一些关于SO的问题(对不起,没有链接了),我找到了一些代码:

- (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文件。

您可以告诉您的应用使用旧的“新闻记录模式”:


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

Source

我不确定这是否可以解决您的问题,但这是iOS 7中Core Data发生的大变化

如果您想了解有关WAL的更多信息,建议您观看WWDC session #207, "Whats New In Core Data & iCloud"

10-08 07:44
查看更多