如何将领域数据库迁移到应用程序组? [英] How to migrate a realm database to an app group?

查看:96
本文介绍了如何将领域数据库迁移到应用程序组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在应用和扩展程序之间添加了共享Realm数据,真的很兴奋。该文档详细说明了如何将默认域设置为app组目录,我已经开始工作了。

Really excited about the recent addition of sharing Realm data between apps and extensions. The documentation details how to set the default realm to the app group directory, I've got that working.

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

Here's what I'm stuck on -- what's the best way to transfer the old database to the new location in the app group?

推荐答案

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

Based on @segiddins comment, I decided to go with moving the old database to the app group using 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)

这篇关于如何将领域数据库迁移到应用程序组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆