realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Failing to open synced realm after problems with internet.

Open destman opened this issue 8 years ago • 20 comments

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

  1. call Realm.getInstanceAsync with waitForInitialRemoteData() set in configuration.
  2. 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.
  3. Restore connection to internet
  4. Try to connect again with same configuration.
  5. 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 avatar Jan 12 '18 11:01 destman

@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:

  1. Kill app, change something on the ROS (by Realm Studio)
  2. Turn off internet on the device
  3. Open app, open realm, etc.
  4. Turn on internet, but not sync action is executed

wtopolski avatar Jan 22 '18 12:01 wtopolski

@wtopolski even with waitForInitialRemoteData()?

Zhuinden avatar Jan 22 '18 13:01 Zhuinden

In 4.3.3 if you turn off internet while initial data is downloading nothing is called in callback...

destman avatar Jan 22 '18 13:01 destman

@nhachicha

Zhuinden avatar Jan 22 '18 13:01 Zhuinden

@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

wtopolski avatar Jan 22 '18 14:01 wtopolski

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.

nhachicha avatar Jan 29 '18 16:01 nhachicha

I'm guessing @nhachicha meant to tag @wtopolski

wmbest2 avatar Jan 29 '18 17:01 wmbest2

yes sorry @wmbest2 🙏

nhachicha avatar Jan 29 '18 19:01 nhachicha

@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 avatar Jan 30 '18 09:01 destman

@destman Is this the scenario that you're experiencing?

  1. Login or use a current SyncUser
  2. Start your loading UI
  3. Build a SyncConfiguration
  4. You turn off the internet (you can use a breakpoint at this step, then activate flight mode)
  5. Use getInstanceAsync with a callback to open the Realm
  6. No callbacks are invoked (onSuccess or onError)

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 avatar Jan 30 '18 13:01 nhachicha

@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...

destman avatar Jan 30 '18 13:01 destman

@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

wtopolski avatar Jan 31 '18 08:01 wtopolski

@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 avatar Jan 31 '18 16:01 nhachicha

@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.

wtopolski avatar Feb 02 '18 08:02 wtopolski

Thanks @wtopolski please send them to [email protected]

nhachicha avatar Feb 03 '18 12:02 nhachicha

@nhachicha I have just sent you email. If you need any other information please tell me.

wtopolski avatar Feb 03 '18 20:02 wtopolski

@nhachicha Hi, I see the issue is closed. Have you received my e-mail? Was it helpful?

wtopolski avatar Feb 05 '18 18:02 wtopolski

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 avatar Feb 05 '18 18:02 nhachicha

@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.

wtopolski avatar Feb 06 '18 07:02 wtopolski

Realm4.3.4 released

nhachicha avatar Feb 06 '18 16:02 nhachicha