realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.47k stars 1.75k forks source link

Realm Sync not happening when app started without data connectivity. Using Realm SDK for Java #7818

Closed theclinkapp closed 10 months ago

theclinkapp commented 1 year ago

How frequently does the bug occur?

Always

Description

Im using Java Realm SDK. The app works fine when network connectivity is available. App even works when i turn off data connectivity after opening app. But when i restart app with no data connectivity. It is unable to fetch the local data.

How to Reproduce: Turn off data connectivity. Open app App unable to sync.

What I got after diagnosing:

After I restart the app without data connectivity. The app is unable to set SyncConfiguration as a result realm is not able to fetch any data. Hence as Mongo says it is offline first I’m able to use this important feature.

But when I turn off data while running app. the app works fine. as SyncConfiguration is already set.

This is My sync configuration:

config = new SyncConfiguration.Builder(app.currentUser())
                    .allowWritesOnUiThread(true)
                    .allowQueriesOnUiThread(true)
                    .compactOnLaunch()
                    .waitForInitialRemoteData(500, TimeUnit.MILLISECONDS)
                    .initialSubscriptions(new SyncConfiguration.InitialFlexibleSyncSubscriptions() {
                        @Override
                        public void configure(Realm realm, MutableSubscriptionSet subscriptions) {
                            // add a subscription with a name
                            Log.e(TAG, "configure: ");

                            Subscription userSubscription = subscriptions.find(kMap.userSubs);
                            if (userSubscription == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.userSubs,
                                        realm.where(users.class)));
                            }

                            Subscription collegeSubscription = subscriptions.find(kMap.collegeSubs);
                            if (collegeSubscription == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.collegeSubs,
                                        realm.where(colleges.class)));
                            }

                            Subscription courseSubscription = subscriptions.find(kMap.courseSubs);

                            if (courseSubscription == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.courseSubs,
                                        realm.where(courses.class)));
                            }

                            Subscription studentsSubs = subscriptions.find(kMap.studentsSubs);
                            if (studentsSubs == null) {
                                subscriptions.addOrUpdate(Subscription.create(kMap.studentsSubs,
                                        realm.where(students.class)));
                            }

                            realm.close();
                        }
                    })
                    .build();

            Realm.setDefaultConfiguration(config);
            StaticValues.syncCount++;

Stacktrace & log output

Error I get while internet is off:

E/REALM_JAVA: Session Error[wss://realm.mongodb.com/]: UNKNOWN(realm.util.network.resolve:1): Host not found (authoritative)
E/REALM_SYNC: Failed to resolve ‘ws.ap-south-1.aws.realm.mongodb.com:443’: Host not found (authoritative)

Can you reproduce the bug?

Always

Reproduction Steps

Turn off data connectivity. Open app App unable to sync. No response

Version

10.10.1

What Atlas App Services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

Android 11

Build environment

Android Studio version: ... Android Build Tools version: ... Gradle version: ...

cmelchior commented 1 year ago

Hi @theclinkapp

Judging from just this sample it should work, so something else must be going on.

1) A first guess is that either something is wrong with the sync connection in general. Have you tried adding a SyncConfiguration.Builder.syncErrorHandler() and see if it reports anything?

2) The second guess is that something is wrong with the initial subscriptions somehow and that is causing them to stop working. Does realm.subscriptions.errorMessage return anything?

github-actions[bot] commented 10 months ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.