在iOS 7中预加载数据库 [英] Preloading a database in iOS 7
问题描述
在过去,我已经释放了我的应用程序与预加载的数据库,所以用户不必在第一次运行时更新它。有一些代码我发现在另一个问题SO(对不起,没有链接了),我添加到我的应用程序代表的 persistentStoreCoordinator
方法:
In the past I have released my app with a database that was preloaded, so the user didn't have to update it on the first run. There was some code I found in another question on SO (sorry, don't have the link anymore) that I added to my App Delegate's persistentStoreCoordinator
method:
- (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文件)。我还需要对这些文件做一些事情吗?或者,它不再可能有一个预装载的数据库与应用程序?
When I try to do this in iOS 7, I don't get any errors, but the database is empty (even though the database in my mainBundle
has all the info I'm expecting). I noticed that there are more database files (a .sqlite-shm file and a .sqlite-wal file) in the applicationDocumentsDirectory
. Do I need to do something with those files as well? Or is it no longer possible to have a preloaded database ship with the app?
编辑:我尝试添加代码复制新的.sqlite-shm和.sqlite-wal
I tried adding code to copy the new .sqlite-shm and .sqlite-wal files as well, but that doesn't help.
推荐答案
核心数据在iOS 7中有所改变,主要是如何保存
Core Data has changed a bit in iOS 7, mainly how saves are performed.
写入日志(wal)是为了提高性能而引入的,因此您看到的WAL sqlite文件。
Write Ahead Logging (wal) was introduced to improve performance, hence the WAL sqlite file you see.
您可以告诉您的应用程序使用旧的日志模式:
You can tell your app to use the old 'journal mode':
您可以通过添加NSSQLitePragmasOption
调用
时的选项addPersistentStoreWithType:configuration:url:options:error。例如。 to
设置先前的默认模式DELETE:
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"} };
我不知道这是否会解决您的问题,但它是iOS 7中的核心数据发生了重大变化
I am not sure if this will fix your issue, but it is the big thing that has changed with Core Data in iOS 7
如果您想了解有关WAL的更多信息,建议您观看 WWDC会议#207,Core Data& iCloud中的新功能
If you want to learn more about WAL, I suggest watching WWDC session #207, "Whats New In Core Data & iCloud"
这篇关于在iOS 7中预加载数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!