Warning message: init(user:realmURL is deprecated


#1

We are getting the following warning

warning message: init(user:realmURL:enableSSLValidation:isPartial:urlPrefix:)’ is deprecated: Use SyncUser.configuration()

With v 3.7.0 I see
+[RLMSyncConfiguration initWithUser] has been deprecated in favor of-[RLMSyncUser configurationWithURL:url].

Can we have some clarification on the use of SyncUser.configuration() and what the proper methods are to authenticate? I scrubbed through the examples and didn’t find a good Swift use case of the proper implementation.

Edit
It’s been 28 days with no response but I’ve found a similar question on StackOverflow


#2

@jay Our API docs are always a great resource - see the SyncUser.configuration definition here -

https://realm.io/docs/swift/3.7.6/api/Extensions/SyncUser.html#/s:So11RLMSyncUserC10RealmSwiftE13configurationAC0C0C13ConfigurationV10Foundation3URLVSg05realmH0_Sb19fullSynchronizationSb19enableSSLValidationSSSg9urlPrefixtF


#3

Yes, as the stackoverflow issue/comments say you will want to use SyncUser.current.configuration. Only the first parameter is required, meaning the others can be removed if you are going to use their defaults. :grinning:


#4

The issue is the followup question from the post on SO;

have you tried modifying an existing synced Realm to use this configuration?

either doing this

let config = SyncUser.current!.configuration(realmURL: url, fullSynchronization: false, enableSSLValidation: true, urlPrefix: nil)

or this

let config = SyncUser.current!.configuration()

Then trying to access realm does not sync. Maybe the missing piece is how to define what objects sync. It used to be like this

let realmConfig = Realm.Configuration(syncConfiguration: config, objectTypes:[MyObject.self])
let realm = try! Realm(configuration: self.realmConfig)

From the current documentation here

https://docs.realm.io/platform/using-synced-realms/opening-a-synced-realm#manually-configuring-synced-realms

It states

Realms on the Realm Object Server are using the same Realm.Configuration used to create standalone Realms, but with the syncConfiguration property on their Realm.Configuration set to a SyncConfiguration value. Synchronized realms are located by URLs.

// Create the configuration
let syncServerURL = URL(string: “realms://myinstance.cloud.realm.io/~/userRealm”)!
let config = user.configuration(realmURL: syncServerURL);

// Open the remote Realm
let realm = try! Realm(configuration: config)
// Any changes made to this Realm will be synced across all devices!

however SyncConfiguration is depreciated.

This is very circular.


#5

Answering my own question, and this is in reference to a partial or query based sync.

I created two files in my project - one is a struct to hold my static URL data and the other as a class service I can use throughout my app to connect to realm

This first one is from the Realm Documentation

struct Constants {
    static let MY_INSTANCE_ADDRESS = "my_project.cloud.realm.io" // <- update this
    
    static let AUTH_URL  = URL(string: "https://\(MY_INSTANCE_ADDRESS)")!
    static let REALM_URL = URL(string: "realms://\(MY_INSTANCE_ADDRESS)/ToDo")!
}

this is the class that can be called throughout the app and resolves the depreciated error message

import RealmSwift
var RealmService = ReferenceRealm.shared.realm

private class ReferenceRealm {
    static let shared = ReferenceRealm()
    
    lazy var realm: Realm = {
        let syncUserConfig = SyncUser.current?.configuration(realmURL: Constants.REALM_URL, fullSynchronization: false, enableSSLValidation: true)
        let realm = try! Realm(configuration: syncUserConfig!)
        return realm
    }()
}

Use would be something like this if you had a managed Realm object calls Tasks for the ToDo project

    func handlePrintTasks() {
        let tasks = RealmService.objects(Tasks.self)
        let tasksSubscription = tasks.subscribe()
        for task in tasks {
            let itemId = task.itemId
            let taskBody = task.body
            print(itemId, taskBody)
        }
    }