Failing to open synced realm after problems with internet.
Goal
Open synced realm using Realm.getInstanceAsync.
Expected Results
onSuccess called in provided callback
Actual Results
onError called with Throwable where message is set to "UNKNOWN(-1) It was not possible to download all remote changes. Has the SyncClient been started?"
Steps & Code to Reproduce
- call Realm.getInstanceAsync with waitForInitialRemoteData() set in configuration.
- if something happened while data is downloading (internet connection lost, or user disable wifi or enter airplane mode) onError called with proper message. This works as expected.
- Restore connection to internet
- Try to connect again with same configuration.
- onError called with message that downloading is not possible.
If you restart application - all works fine.
Code Sample
RealmConfiguration config = new SyncConfiguration.Builder(user, getServerURL()).schemaVersion(SCHEMA_VERSION).disableSSLVerification().waitForInitialRemoteData().build();
Realm.getInstanceAsync(config, new Realm.Callback() {
@Override
public void onError(@NonNull Throwable exception) {
user.logout();
onFinished("migrateRealm exception: " + exception.getMessage());
}
@Override
public void onSuccess(@NonNull Realm realm) {
Realm localRealm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(...);
realm.commitTransaction();
realm.close();
localRealm.close();
onFinished(null);
}
});
Version of Realm and tooling
Realm version(s): 4.3.1 Realm sync feature enabled: yes Android Studio version: 3.0.1 Which Android version and device: any
@destman Try to switch to Realm 4.3.2 (or newest version 4.4.3 - minor fix). There is a bug related with lack of Internet in 4.3.1. Unfortunately in 4.3.2 I have issue like that:
Steps:
- Kill app, change something on the ROS (by Realm Studio)
- Turn off internet on the device
- Open app, open realm, etc.
- Turn on internet, but not sync action is executed
@wtopolski even with waitForInitialRemoteData()?
In 4.3.3 if you turn off internet while initial data is downloading nothing is called in callback...
@nhachicha
@Zhuinden My code:
val config = SyncConfiguration.Builder(user, serverUrl)
.waitForInitialRemoteData()
.readOnly()
.modules(RealmInitialModule())
.build()
Realm.getInstanceAsync(config, object : Realm.Callback() {
override fun onSuccess(realm: Realm?) {
...
}
override fun onError(exception: Throwable?) {
...
}
})
On 4.3.2 when I lost connection for a while after successful start, realm will reconnect correctly. But if I start app in offline mode, realm doesn't sync content. My log trace looks like that: https://github.com/realm/realm-java/issues/5579#issuecomment-358961306
Hi @wmbest2 @destman I couldn't reproduce the issue, I created a sample project that uses sync with your configuration https://www.dropbox.com/s/8o7k0oa50o3jet6/SyncIntro.zip?dl=0 can you see if you manage to reproduce it?
Note: you need to set your IP address in Constants#INSTANCE_ADDRESS
The app tries to connect if there's a current user, otherwise, attempt a connection with an admin user (test password test) that you need to create using Studio for example.
I'm guessing @nhachicha meant to tag @wtopolski
yes sorry @wmbest2 🙏
@nhachicha Sync works fine in 4.3.3. The problem that stays - if you log to server (or get valid user credentials any other way) and try to open sync database - noting is called in callback (only when internet disappears at this stage). In application we show view that block UI until realm return success or failure... Maybe there is some timeout option that i miss ?
@destman Is this the scenario that you're experiencing?
- Login or use a current
SyncUser - Start your loading UI
- Build a
SyncConfiguration - You turn off the internet (you can use a breakpoint at this step, then activate flight mode)
- Use
getInstanceAsyncwith a callback to open the Realm - No callbacks are invoked (
onSuccessoronError)
If this is the case then yes, onSuccess or onError will not be called since we keep getting java.net.ConnectException and we do retries in the background indefinitely using our ExponentialBackoffTask class, hence the callbacks are not invoked.
A workaround could be to use getInstance if possible, since this will return the Realm, so you can still work on it while offline, then it will synchronise once the Internet is back.
@nhachicha Yes! Using getInstance is not a good way to solve the problem because we need to fetch initial data to properly merge existing user data and data on server...
@nhachicha Thank you for response. Sorry for delay, I have just noticed your response. I need 1-2 days to test your sample app. My scenario is different then you described:
1 ) Turn on Internet 2) Login or use a current SyncUser, full sync 3) Kill app 4) You turn off the internet 5) Open app and use a current SyncUser 6) Start your loading UI 7) Build a SyncConfiguration 8) Use getInstanceAsync with a callback to open the Realm, and I get realm with cached data, that's OK 9) Change something in Realm Studio 10) You turn on the internet 11) No changes in the app, no sync operation
@destman then you can design your UI such as you indicate the connection status (offline/online) so the user knows that while s/he's offline you can't obviously download the initial data and proceed to the next screen.
IMO If you rely on the initial data and you're offline you can only inform the user, this is the same use case if you want to connect the first time, you can't perform the connection and get the SyncUser if you're offline (you can only inform the app user about this)
As a side note, Realm could also be enhanced to reduce the waiting time of the exponential back off this is tracked in https://github.com/realm/realm-java/issues/5735
@nhachicha
Hi, I have checked your sample app, and I could say that it always works on emulator, but I can also reproduce this bug on device. I tested it on Moto G with 5.1 and Nexus 6p with 8.1. I recorded logs, and movie to show it. How can I send you those files? I prefer private channel. If you want any other stuff please tell me.
Thanks @wtopolski please send them to [email protected]
@nhachicha I have just sent you email. If you need any other information please tell me.
@nhachicha Hi, I see the issue is closed. Have you received my e-mail? Was it helpful?
Hi @wtopolski thanks for the detailed report. I was able to fix the issue, can you please try the 4.4.0-SNAPSHOT release to double check the fix worked. I'll do a patch release afterwards. cc @destman
@nhachicha It's ok. There is no sync problem on version 4.4.0-SNAPSHOT. Thank you for the fix.
Probably you can also close issue #5579, it is the same problem I think.
Realm4.3.4 released