How to transfer an area database to an application group?

Really worried about the recent addition of Realm data sharing between applications and extensions. The documentation describes in detail how to set the default scope in the application group directory, it works for me.

Here's what I'm stuck with - what is the best way to move the old database to a new location in the application group?

+6
source share
2 answers

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

Hope this helps another reader.

As discussed at https://github.com/realm/realm-cocoa/issues/4490 , you can set the path to the application group using the code below and use File Manager to move the existing db, as mentioned above.

 var config = Realm.Configuration() config.fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)!.appendingPathComponent(dbFilename) Realm.Configuration.defaultConfiguration = config 
0
source

Source: https://habr.com/ru/post/986391/


All Articles