今日の拡張機能のために、既存のレルムデータベースファイルをアプリグループの場所にコピーします

May 06 2020

iOSアプリがレルムデータベースを使用しています。現在のdefault.realmデータベースファイルを新しいディレクトリ(アプリグループの場所)にコピーまたは移動して、TodayExtensionウィジェットと共有できるようにします。

この投稿が言うように私は試しました(レルムデータベースをアプリグループに転送する方法)

コアコードは

let fileManager = FileManager.default
let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myApp")!.appendingPathComponent("default.realm")
do{
    try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
}
catch{
    print("Error information: \(error)")
}

そして、このコードを次のように、didFinishLaunchingWithOptions内のレルム移行スコープ内に配置しました。このコードを使用している場所をより明確に示すためです。


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    var config = Realm.Configuration(
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion < 1) {
                let fileManager = FileManager.default
                let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
                let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myApp")!.appendingPathComponent("default.realm")
                do{
                    try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
                }
                catch{
                    print("Error information: \(error)")
                }
            }
    }
    )
}


これを試してみると、コンソールにファイルを保存できなかったと表示されます。

エラードメイン= NSCocoaErrorDomainコード= 512「ファイル「default.realm」をフォルダ「2EEADCEE-F9D9-44A8-BDED-B60A689656A2」に保存できませんでした。」UserInfo = {NSFileOriginalItemLocationKey = file:/// Users / jm / Library / Developer / CoreSimulator / Devices / 38334AE3-6648-402E-AC18-8252426002D6 / data / Containers / Shared / AppGroup / 2EEADCEE-F9D9-44A8-BDED-B60A689656A2 / default.realm、.....。

レルムデータベースファイルのコピー/移動はファイルを開く前に行う必要があると聞きましたが、このエラーはそれに関連していますか?

あなたの助けをありがとうそして素晴らしい一日を

回答

2 JM_Developer May 16 2020 at 01:53

私が道を見つけるのを助けてくれたコメントをありがとう@Jay。

彼が述べたように、レルムDBファイルの移動は、レルムに関連するものを呼び出す前に実行する必要があります。

もともと、スキーマのバージョンが古い場合に1回だけ実行されるように、レルム移行内にコードがありました。

しかし、didFinishLaunchingWithOptionsの上部に移動したので、RealmDBファイルを別のディレクトリに移動できます。

これが苦労している誰かを助けることを願っています。


let fileManager = FileManager.default
let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.myApp")!.appendingPathComponent("default.realm")
if fileManager.fileExists(atPath: originalPath.absoluteString) {
    do{
        try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
        print("Successfully replaced DB file")
    }
    catch{
        print("Error info: \(error)")
    }
} else {
    print("File is not exist on original path")
}


var config = Realm.Configuration(
)
config.fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.myApp")!.appendingPathComponent("default.realm")

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()