gpt4 book ai didi

ios - 如何将预先存在的 sqlite 文件导入核心数据 iOS 7.1

转载 作者:行者123 更新时间:2023-11-29 10:43:55 27 4
gpt4 key购买 nike

我创建了一个新的核心数据项目并设置了我的核心数据模型。然后我在模拟器中运行它,然后保存上下文。然后我使用 Core Data Editor 5 打开并查看我的数据库。我添加并编辑了新的权利,保存了文件,然后我进入模拟器检查它是否有效并且所有内容都已添加并且看起来很好。我现在想将此数据库添加到我的项目包中,并让应用程序加载此 bata 数据库作为其默认核心数据库。使用新的 wal 系统我似乎无法让它工作。我想知道是否有人知道如何修复它。我听说您必须添加所有 3 个文件(.sqlite、wal、shm),但我不知道将其保存在何处或通过什么过程来完成。

最佳答案

将这三个资源作为资源添加到您的 iOS 应用程序中。然后

NSURL *storeURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"myfilename" ofType:@"myfileextension"]];

并在调用 -addPersistentStoreWithType:configuration:... 时将其传递给您的 NSPersistentStoreCoordinator

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

NSURL *storeURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"myfilename" ofType:@"myfileextension"]];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:@{NSReadOnlyPersistentStoreOption : @YES,
NSSQLitePragmasOption: @{ @"journal_mode" : @"WAL"}}
error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
*/

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return _persistentStoreCoordinator;
}

虽然给了你那个片段,但我不是这样做的,我不知道它是否会起作用。相反,如果我正在创建一个静态数据存储以用作只读数据,我将在创建数据存储(在另一个工具或程序中)和读取它时使用旧的 DELETE 日志模式我实际的 iOS 应用程序。这意味着更改 options: 参数(在创建代码和读取代码上)。

              options:@{NSReadOnlyPersistentStoreOption : @YES,
NSSQLitePragmasOption: @{ @"journal_mode" : @"DELETE"}}

http://www.sqlite.org/draft/wal.html声明“无法打开只读 WAL 数据库。”在 http://www.sqlite.org/draft/wal.html#readonly 进一步讨论.

关于ios - 如何将预先存在的 sqlite 文件导入核心数据 iOS 7.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23141326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com