Realm Cloud and Encryption


#1

I have an iOS app in beta on TestFlight and am using Realm Cloud for syncing. Before I get to my question, I just want to say I love how fast this service is. Keep up the awesome work.

Is there an easy way to enable encryption for apps that utilize Realm Cloud? Are apps that are using full sync have encryption automatically enabled? I understand how to enable it for a local-only realm but when it’s hooked up to Realm Cloud, there is no longer a local realm file (at least I can’t find one). There must be at least a local cache somewhere on the device because I can use the app offline and the changes automatically sync when I turn off airplane mode. I’ve read several forum posts about this and haven’t found a working solution. My current realm configuration is as follows:

struct MyRealmConfig {
    static func getRealm() -> Realm {
        do {
            let realmConfig = SyncUser.current!.configuration(realmURL: RealmConstants.REALM_URL, fullSynchronization: true)
            let realm = try Realm(configuration: realmConfig)
            return realm
        } catch {
            fatalError()
        }
    }
}

#2

@joshRond It is literally the same configuration as a non-synced Realm. Just pass in an encryptionKey into the RealmConfiguration struct


#3

Can you confirm, using the configuration I presented in my initial post, does this configuration store a local cache/realm file somewhere on the device? I can’t find it using the program SimPholders or when I attempt to print the fileURL to the console, it comes back nil.

To address your suggestion, I can’t use the syncConfiguration property in the RealmConfig struct because it’s deprecated (see below) which is why I used the configuration I displayed in the initial post. The SyncUser.configuration function doesn’t have an encryption property to set.


#4

@joshRond A Configuration struct can take a SyncConfiguration struct as a property - see API docs here:

https://realm.io/docs/swift/latest/api/Classes/Realm/Configuration.html


#5

I’m not a swift expert, but I believe you can simply set the encryption key after creating the config object:

let realmConfig = SyncUser.current!.configuration(realmURL: RealmConstants.REALM_URL, fullSynchronization: true)
realmConfig.encryptionKey = ...
let realm = try Realm(configuration: realmConfig)

And yes, synchronized Realms are backed by a local Realm that contains the data for offline use.


#6

Yes, that is exactly what I am looking for. Thank you.