itabdullah
itabdullah

Reputation: 607

Realm SyncConfiguration.Builder deprecated

I am using Realm Cloud Sync and I used to write following code for logging in to server.

if (SyncUser.current() == null) {
    SyncCredentials myCredentials = SyncCredentials.usernamePassword("userid", "pass", false);
    SyncUser syncUser = SyncUser.logIn(myCredentials, AUTH_URL);
}
SyncConfiguration configuration = new SyncConfiguration.Builder(SyncUser.current(), REALM_BASE_URL + "/~/myDB").build();
Realm.setDefaultConfiguration(configuration);

Then after realm plugin update to version 5.3.1, the SyncConfiguration.Builder command got deprecated. It is now suggesting to use SyncUser.createConfiguration. Can I have an example of how to use this command?

I tried using

Realm.setDefaultConfiguration(SyncUser.current().createConfiguration(REALM_BASE_URL + "/~/myDB").build());

However the login wasn't successfull.

Thanks.

Upvotes: 1

Views: 437

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81588

You need to log in first to have a valid SyncUser.

    SyncUser user = SyncUser.logIn(SyncCredentials.usernamePassword(username, password, true), Constants.AUTH_URL);

    SyncConfiguration config = user.createConfiguration(Constants.USER_REALM).build();

So I'm guessing the following would work:

SyncUser syncUser = SyncUser.current();
if (syncUser == null) {
    SyncCredentials myCredentials = SyncCredentials.usernamePassword("userid", "pass", false);
    syncUser = SyncUser.logIn(myCredentials, AUTH_URL);
}
SyncConfiguration configuration = syncUser.createConfiguration(REALM_BASE_URL + "/~/myDB").build();

Note that this defaults to using partial synchronization, so if you want to retain previous behavior, you might have to call .fullSynchronization().build().

Upvotes: 2

Related Questions