【问题标题】:Using an existing SQLite database in MagicalRecord (swift)在 MagicalRecord (swift) 中使用现有的 SQLite 数据库
【发布时间】:2014-11-02 15:29:42
【问题描述】:

我希望我的 iOS swift 应用程序能够在 MagicalRecord 中预加载现有的 sqlite 文件。
参考this等,
将 sqlite, sqlite-shm, sqlite-wal 文件添加到我的项目中,像这样在 didFinishLaunchingWithOption 中编写了一些代码。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //  copy initial sqlite file to application directory
    let preloadSQLiteURL:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myData", ofType: "sqlite")!)
    let preloadShmURL:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myData", ofType: "sqlite-shm")!)
    let preloadWalURL:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myData", ofType: "sqlite-wal")!)

    let storeSQLiteURL:NSURL = NSPersistentStore.MR_urlForStoreName("myData.sqlite")
    let storeShmURL:NSURL = NSPersistentStore.MR_urlForStoreName("myData.sqlite-shm")
    let storeWalURL:NSURL = NSPersistentStore.MR_urlForStoreName("myData.sqlite-wal")

    if !NSFileManager.defaultManager().copyItemAtURL(preloadSQLiteURL, toURL: storeSQLiteURL, error:&err) {
        println("failed to copy sqlite file.")
    }
    if !NSFileManager.defaultManager().copyItemAtURL(preloadShmURL, toURL: storeShmURL, error:&err) {
        println("failed to copy shm file.")
    }
    if !NSFileManager.defaultManager().copyItemAtURL(preloadWalURL, toURL: storeWalURL, error:&err) {
        println("failed to copy wal file.")
    }
    MagicalRecord.setupCoreDataStackWithAutoMigratingSqliteStoreNamed("myData.sqlite")

    return true
}

但是 copyItemAtURL 都失败了。

路径的价值是这些。
preloadSQLiteURL = file:///var/mobile/Applications/XXX/myApp.app/myData.sqlite
preloadShmURL = file:///var/mobile/Applications/XXX/myApp.app/myData.sqlite-shm
preloadWalURL = file:///var/mobile/Applications/XXX/myApp.app/myData.sqlite-wal
storeSQLiteURL = file:///var/mobile/Applications/XXX/Documents/myData.sqlite
storeShmURL = file:///var/mobile/Applications/XXX/Documents/myData.sqlite-shm
storeWalURL = file:///var/mobile/Applications/XXX/Documents/myData.sqlite-wal

我的环境是Xcode6 beta7,iOS7.1.3。 有人请帮助我吗?我该怎么办? 任何建议表示赞赏。提前致谢。

【问题讨论】:

    标签: ios sqlite core-data swift magicalrecord


    【解决方案1】:

    结果,自己解决了。

    NSError 显示“操作无法完成。(Cocoa 错误 516)”
    Cocoa 错误 516 是“NSFileWriteFileExistsError”的意思是“您不能将文件移动到文件已经存在的地方。”
    所以,我以这种方式更正了我的代码。

    let fileManager = NSFileManager.defaulManager()
    let preloadSQLiteURL:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myData", ofType: "sqlite")!)
    let storeSQLiteURL:NSURL = NSPersistentStore.MR_urlForStoreName("myData.sqlite")
    if fileManager.fileExistsAtPath(storeSQLiteURL.path!) {
        // file already exists, delete it and try again.
        fileManager.removeItemAtURL(storeSQLiteURL, error:&err)
    }
    fileManager.copyItemAtURL(preloadSQLiteURL, toURL:storeSQLiteURL, error:nil)
    

    然后,copyItemAtURL成功完成,sqlite文件生效。

    【讨论】:

      【解决方案2】:

      虽然这是一个措辞不当的问题,但我假设您希望将应用程序包中的 SQLite 数据库文件加载到用户的文档目录中,以便您可以从该数据开始,然后从那里进行更新。

      但是,您在正确的路径上,而不是复制所有 3 个文件,当您创建默认数据存储文件时,请使用 NSSQLitePragmasOption 将您的 journal_mode 设置为“DELETE”:

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

      在创建默认数据集时将持久存储添加到协调器时使用这组选项只会创建一个 SQLite 文件。从那里,只复制一个文件会更容易。

      我还在您的代码中注意到您正在复制 myData.sqlite,但您正在尝试加载“acData.sqlite”。您是否尝试加载不存在的文件?

      【讨论】:

      • 感谢补充。是的,我想将应用程序包中的 SQLite 数据库文件加载到 MagicalRecord/CoreData 使用的默认目录中。而“acData.sqlite”只是错字。感谢您的意见。我试试看。
      猜你喜欢
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 2013-03-14
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      相关资源
      最近更新 更多