Realm-java: Realm data is not synced between different devices

Created on 1 Jun 2017  Â·  11Comments  Â·  Source: realm/realm-java

I'm using syncconfiguration and ROS to sync data between two devices. Initially, the data was able to syn without any trouble. But after I modified the schema and created a new Realm, when I save the data in the first device, the second device cannot syn the data. Even though the data was saved in the server successfully.

So I did some simple tests. When I uninstall and reinstall the app in the first device, the data was also not synced initially, but if I wrote anything new, the data got synced. Did I miss anything in the sync configuration?

Also, when I query, I can get the correct results but an error showed up.

06-01 09:47:57.144 D/REALM_JAVA: AuthenticateResponse - Error: UNKNOWN(-1)
                                 java.io.InterruptedIOException: thread interrupted
                                     at okio.Timeout.throwIfReached(Timeout.java:146)
                                     at okio.Okio$1.write(Okio.java:75)
                                     at okio.AsyncTimeout$1.write(AsyncTimeout.java:180)
                                     at okio.RealBufferedSink.flush(RealBufferedSink.java:216)
                                     at okhttp3.internal.http.Http1xStream.finishRequest(Http1xStream.java:161)
                                     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:51)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
                                     at okhttp3.RealCall.execute(RealCall.java:60)
                                     at io.realm.internal.network.OkHttpAuthenticationServer.authenticate(OkHttpAuthenticationServer.java:110)
                                     at io.realm.internal.network.OkHttpAuthenticationServer.loginToRealm(OkHttpAuthenticationServer.java:63)
                                     at io.realm.SyncSession$2.execute(SyncSession.java:225)
                                     at io.realm.SyncSession$2.execute(SyncSession.java:221)
                                     at io.realm.internal.network.ExponentialBackoffTask.run(ExponentialBackoffTask.java:66)
                                     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                     at java.lang.Thread.run(Thread.java:818)

Version of Realm and tooling

Realm version(s): 3.1.1

Realm sync feature enabled: yes

Android Studio version: 2.3.2

Android version and device: Samsung S6 (Android 6.0.1), Lenovo Phablet (Android 6.0.1)

T-Help

Most helpful comment

Thanks, so let me summarize what shall I do.

  1. update realm to at least 3.1.3?
  2. open realm in onStart() and close in onStop() in activity
  3. add change listener to update every realm schema at onStart() ?

All 11 comments

the only time I ever saw this was when the local Realm instance was closed before the sync was complete, is this application to your codebase?

Yes. So I shouldn't close the realm instance right after query?

Well if you're closing them then I'm sure you can't add RealmChangeListener to them, which would mean you can't update when the synchronization occurs from other devices, so that sounds tricky :smile:

But yeah, see https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances

I was trying to follow the https://github.com/realm-demos/realm-tasks
In the demo, it closed the realm everytime after query without any errors, and the data was also synced between devices

That is a bit tricky because the master sample uses 2.3.0, but there were changes to be handled in 3.1.*, see https://github.com/realm-demos/realm-tasks/pull/405

(I know, I ran into it)

Thanks, so let me summarize what shall I do.

  1. update realm to at least 3.1.3?
  2. open realm in onStart() and close in onStop() in activity
  3. add change listener to update every realm schema at onStart() ?

@Insane-neal The important thing is matching a pair of open realm and close it always. You can also use Realm Java 3.3.1.

The issue was solved. The error was because the realm was closed in every function. Now I am following @Zhuinden suggestion to manage the realm instance in onStart and onStop in an Activity.

The realm not syncing issue was solved by adding waitForInitialRemoteData in the sync configuration. It wasn't available in the version I was using (3.1.1)

Please re-open if you encounter this again. Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yuwu picture yuwu  Â·  3Comments

nolanamy picture nolanamy  Â·  3Comments

mithrann picture mithrann  Â·  3Comments

pawlo2102 picture pawlo2102  Â·  3Comments

wezley98 picture wezley98  Â·  3Comments