ios - 如何将 Realm 数据库迁移到应用程序组?

标签 ios swift realm

对于最近在应用程序和扩展程序之间共享 Realm 数据感到非常兴奋。该文档详细说明了如何将默认 Realm 设置为应用程序组目录,我已经做到了。

这就是我坚持的问题 -- 将旧数据库转移到应用程序组中的新位置的最佳方法是什么?

最佳答案

根据@segiddins 的评论,我决定使用 NSFileManager 将旧数据库移动到应用程序组:

    let fileManager = NSFileManager.defaultManager()

    //Cache original realm path (documents directory)
    let originalDefaultRealmPath = RLMRealm.defaultRealmPath()

    //Generate new realm path based on app group 
    let appGroupURL: NSURL = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.AppGroup")!
    let realmPath = appGroupURL.path!.stringByAppendingPathComponent("default.realm")

    //Moves the realm to the new location if it hasn't been done previously
    if (fileManager.fileExistsAtPath(originalDefaultRealmPath) && !fileManager.fileExistsAtPath(realmPath)) {

        var error: NSError?
        fileManager.moveItemAtPath(originalDefaultRealmPath, toPath: realmPath, error: &error)

        if (error != nil) {
            println(error)
        }
    }

    //Set the realm path to the new directory
    RLMRealm.setDefaultRealmPath(realmPath)

关于ios - 如何将 Realm 数据库迁移到应用程序组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29996999/

相关文章:

arrays - 如何在swift中使用zip函数转置

android - default.realm 文件不显示在 chrome 浏览器中

ios - 在 Swift 应用程序中安装 Realm

ios - 如何正确定位动画 CAShapeLayer

javascript - 在 WKWebView(iOS,Swift)中使用本地 Javascript 源加载本地 HTML

ios - 为什么我的矩形画错了?

ios - 无法在路径中打开 Realm

ios - Realm 列表与 LinkingObjects 性能

android - 如何检测iOS用户或Android用户是否“存在”?

ios - CollectionView 无法正常工作