ios – 如何使用swift 3 xcode 8在核心数据中预加载数据库

ios – 如何使用swift 3 xcode 8在核心数据中预加载数据库,第1张

概述我有一个包含数据的数据库,我想在应用程序中预加载它.在 swift 3之前它可以工作,我已经按照本教程: http://www.appcoda.com/core-data-preload-sqlite-database/但是如何为swift 3加载相同的数据库?在介绍NSPersistentContainer时如何加载我项目中的.sqlite文件? 实际上,在swift 3中更改了创建数据库的默认 我有一个包含数据的数据库,我想在应用程序中预加载它.在 swift 3之前它可以工作,我已经按照本教程: http://www.appcoda.com/core-data-preload-sqlite-database/但是如何为swift 3加载相同的数据库?在介绍NSPersistentContainer时如何加载我项目中的.sqlite文件?解决方法 实际上,在swift 3中更改了创建数据库的默认路径.所以现在代码看起来像:

func preloadDBData() {    let sqlitePath = Bundle.main.path(forResource: "MyDB",ofType: "sqlite")    let sqlitePath_shm = Bundle.main.path(forResource: "MyDB",ofType: "sqlite-shm")    let sqlitePath_wal = Bundle.main.path(forResource: "MyDB",ofType: "sqlite-wal")    let URL1 = URL(fileURLWithPath: sqlitePath!)    let URL2 = URL(fileURLWithPath: sqlitePath_shm!)    let URL3 = URL(fileURLWithPath: sqlitePath_wal!)    let URL4 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite")    let URL5 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite-shm")    let URL6 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite-wal")    if !fileManager.default.fileExists(atPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite") {        // copy 3 files        do {            try fileManager.default.copyItem(at: URL1,to: URL4)            try fileManager.default.copyItem(at: URL2,to: URL5)            try fileManager.default.copyItem(at: URL3,to: URL6)            print("=======================")            print("fileS copIED")            print("=======================")        } catch {            print("=======================")            print("ERROR IN copY OPERATION")            print("=======================")        }    } else {        print("=======================")        print("fileS EXIST")        print("=======================")    }}

现在你可以从AppDelegate的dIDFinishLaunchWithOptions方法调用这个方法,这将预加载我们放在应用程序中的数据库.

总结

以上是内存溢出为你收集整理的ios – 如何使用swift 3 xcode 8在核心数据中预加载数据库全部内容,希望文章能够帮你解决ios – 如何使用swift 3 xcode 8在核心数据中预加载数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1033840.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存