Franck
Franck

Reputation: 9319

Realm Object Server. Sync initial Local DB

I have a local Realm database, full of 160000 row. I want to copy it to the local path for Realm to be able to use it as official DB, and sync it online. (so my empty Db will be synced). Can I do that? (at this time, doesn't work because it copy the Db in local folder, but not in the user specific folder)

func loginCompletedA(user: SyncUser) {
        let realmURL = URL(string: “realm://xxx.compute-1.amazonaws.com:9080/beerUsers”)!
        var configuration = Realm.Configuration.defaultConfiguration
        configuration.syncConfiguration = SyncConfiguration(user: user, realmURL: realmURL)
        let defaultURL = configuration.fileURL!
        //let defaultParentURL = defaultURL.deletingLastPathComponent()
        if let v0URL =  Bundle.main.url(forResource: “default”, withExtension: “realm”){
            do {
                //if !ifNotExists {
                try FileManager.default.removeItem(at: defaultURL)
                //}
                try FileManager.default.copyItem(at: v0URL, to: defaultURL)
            } catch {}

            do {
                try FileManager.default.copyItem(at: v0URL, to: defaultURL)
            } catch {}
        }else{

        }


        let realm = try! Realm(configuration: configuration)
        let session = user.session(for: realmURL)!
        downloadToken = session.addProgressNotification(for: .download, mode: .reportIndefinitely) {
            print(“download progress: \($0)“) // This is never called.
        }
        uploadToken = session.addProgressNotification(for: .upload, mode: .reportIndefinitely) {
            print(“upload progress: \($0)“) // This is never called.
        }
    }

Upvotes: 0

Views: 788

Answers (1)

TiM
TiM

Reputation: 15991

Just to confirm what I think you're asking. You're pre-bundling a Realm database file, containing 160,000 rows of data along with your app. When a new user logs into the app, the data is then synchronized with their account.

Unsynchronized Realm files and synchronized Realm files are two different file formats, so it's not possible to convert one file to another. Copying a pre-bundled offline Realm to a user-controlled directory and then trying to apply a syncConfiguration object won't do anything.

The easiest solution to this is to make a new synchronized Realm, and then copy the data from the pre-bundled Realm into the synchronized Realm the first time the app launches.

let bundledRealmURL = Bundle.main.url(forResource: “default”, withExtension: “realm”)!

let localConfiguration = Realm.Configuration()
configuration.readOnly = true
configuration.fileURL = bundledRealmURL
let localRealm = try! Realm(configuration: configuration)

let syncConfiguration = Realm.Configuration()
syncConfiguration.syncConfiguration = SyncConfiguration(user: user, realmURL: realmURL)
let syncRealm = try! Realm(configuration: configuration)

let myObjects = localRealm.objects(MyObject.self)

try! syncRealm.write {
    for myObject in myObjects {
         let newObject = MyObject(value: myObject)
         syncRealm.add(newObject)
    }
}

We're exploring ways to make it easier to 'prefill' synchronized Realms for a future release of Realm. :)

Upvotes: 2

Related Questions