【发布时间】:2021-10-17 00:18:53
【问题描述】:
我的目标是在首次应用启动时预加载 Core Data。到目前为止,我运行了一个模拟并用数据填充了核心数据。 (我已经检查了“允许外部存储”)。
我进入 application_support 并复制了:MyApp.sqlite-wal、MyApp.sqlite-shm、.MyApp_SUPPORT/_EXTERNAL_DATA/ 和 MyApp.sqlite。
然后我在我的应用程序包中添加了 MyApp.sqlite 文件,并在我的应用程序委托中添加了此代码:
lazy var persistentContainer: NSPersistentContainer = {
let modelName = "MyApp"
var container: NSPersistentContainer!
container = NSPersistentContainer(name: modelName)
// Preloading
let appName: String = "MyApp"
var persistentStoreDescriptions: NSPersistentStoreDescription
let storeUrl = self.getDocumentsDirectory().appendingPathComponent("MyApp.sqlite")
if !FileManager.default.fileExists(atPath: (storeUrl.path)) {
let seededDataUrl = Bundle.main.url(forResource: appName, withExtension: "sqlite")
try! FileManager.default.copyItem(at: seededDataUrl!, to: storeUrl)
}
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.url = storeUrl
container.persistentStoreDescriptions = [description]
//End Preloading
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
它可以工作,但它似乎找不到保存在外部存储中的图像。它们作为参考存在于 .MyApp_SUPPORT/_EXTERNAL_DATA 中。 我应该在哪里添加参考文献?
加载一切是我的目标。
【问题讨论】:
标签: swift iphone sqlite core-data persistence