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)
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)
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
No, they open Realm in onStart() and close in onStop() see https://github.com/realm-demos/realm-tasks/blob/master/RealmTasks%20Android/app/src/main/java/io/realm/realmtasks/TaskListActivity.java#L66
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.
onStart() and close in onStop() in activityonStart() ?@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
Most helpful comment
Thanks, so let me summarize what shall I do.
onStart()and close inonStop()in activityonStart()?