Syncing data to self-hosted realm from Android Service

ros

#1

Hello all!

Im using realm in a com.google.android.gms.wearable.WearableListenerService. When a new data change is deteced with the right variables, a method in my libary gets called which is responsible for creating a RealmObject and saving it in the self-hosted ROS. The user is logging in with his/her google account. I will supply the code i used below (minus some data).

The problem im getting is that the data is not appearing in the server when viewing it with Realm Studio. The user realm gets created but no objects arrive.

I enabled the verbose log, see below.
I could not find a solution, and i dont know what i need to change according to the log.

Code for putting the object into the server:
CONSTANTS.Realm.REALMS_URL = “realms://” + INSTANCE_ADDRESS + “/~/”;
where the is the designated path for my application.

try(Realm realm = Realm.getInstance(new SyncConfiguration.Builder(SyncUser.current(), CONSTANTS.Realm.REALMS_URL).build())){
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.copyToRealm(heartRateMeasurementObject);
                    }
                });
            }

Code for logging in:

 Realm.init(context);
            checkGoogleLogin(context, new OnTaskDoneInterface<DoubleObjectBundle<GoogleSignInAccount, LoginResult>>() {
                @Override
                public void onSuccess(DoubleObjectBundle<GoogleSignInAccount, LoginResult> returnType) {
                    switch (returnType.getSecondObject()){
                        case NO_EXISTING_USER:
                            onTaskDoneInterface.onFailure(new Exception("No Google account has been logged in."));
                            break;
                        case EXISTING_USER:
                            if (SyncUser.current() == null){
                                SyncCredentials credentials = SyncCredentials.google(returnType.getFirstObject().getIdToken());
                                SyncUser.logInAsync(credentials, AUTH_URL, new SyncUser.Callback<SyncUser>() {
                                    @Override
                                    public void onSuccess(SyncUser user) {
                                        Realm instance = null;
                                        try {
                                            instance = Realm.getInstance( new SyncConfiguration.Builder(user, CONSTANTS.Realm.REALMS_URL).build());
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                        Log.d("RealmSuccessNEW", user.toString());
                                        onTaskDoneInterface.onSuccess(user);
                                        instance.close();

                                    }
                                    @Override
                                    public void onError(ObjectServerError error) {
                                        Log.e("Login error", error.toString());
                                        onTaskDoneInterface.onFailure(error);
                                    }
                                });
                            }
                            else {
                                SyncUser user = SyncUser.current();
                                Log.d("RealmSuccessCurrent", user.toString());
                                onTaskDoneInterface.onSuccess(user);
                            }
                            break;
                        default:
                            onTaskDoneInterface.onFailure(new Exception("Unknown error."));
                            break;
                    }
                }

                @Override
                public void onFailure(Exception exception) {
                    onTaskDoneInterface.onFailure(exception);
                }
            });

Log with issue:
Where Is my instance address.

``
V/REALM_JNI: --> Java_io_realm_RealmFileUserStore_nativeGetCurrentUser
V/REALM_JNI: --> Java_io_realm_RealmFileUserStore_nativeIsActive
V/REALM_JNI: --> Java_io_realm_RealmFileUserStore_nativeIsActive
V/REALM_JNI: --> Java_io_realm_internal_OsSchemaInfo_nativeCreateFromList
–> Java_io_realm_internal_OsRealmConfig_nativeCreate
V/REALM_JNI: --> Java_io_realm_internal_OsRealmConfig_nativeSetInMemory 535659713792
V/REALM_JNI: --> Java_io_realm_internal_OsRealmConfig_nativeEnableChangeNotification 535659713792
–> Java_io_realm_internal_OsRealmConfig_nativeSetSchemaConfig 535659713792
–> Java_io_realm_internal_OsRealmConfig_nativeCreateAndSetSyncConfig 535659713792
–> Java_io_realm_internal_OsRealmConfig_nativeSetSyncConfigSslSettings 535659713792
D/REALM_JAVA: first session created add network listener
V/REALM_JNI: --> Java_io_realm_internal_OsSharedRealm_nativeGetSharedRealm 535659713792
D/REALM_JAVA: Network request (authenticate): https://:9443/auth
V/REALM_JNI: --> Java_io_realm_internal_OsSharedRealm_nativeGetSchemaInfo 534693498880
–> Java_io_realm_internal_OsSharedRealm_nativeSetAutoRefresh 534693498880
V/REALM_JNI: --> Java_io_realm_internal_OsSharedRealm_nativeRegisterSchemaChangedCallback 534693498880
–> Java_io_realm_internal_OsSharedRealm_nativeIsClosed 534693498880
–> Java_io_realm_internal_OsSharedRealm_nativeBeginTransaction 534693498880
V/REALM_JNI: --> Java_io_realm_internal_OsSharedRealm_nativeIsClosed 534693498880
–> Java_io_realm_internal_OsSharedRealm_nativeGetTable 534693498880
V/REALM_JNI: --> Java_io_realm_internal_OsObjectStore_nativeGetPrimaryKeyForObject 534693498880
–> Java_io_realm_internal_OsSchemaInfo_nativeGetObjectSchemaInfo 535659864400
V/REALM_JNI: --> Java_io_realm_internal_OsObjectSchemaInfo_nativeGetProperty 535659735680
–> Java_io_realm_internal_Property_nativeGetColumnIndex 535660766160
–> Java_io_realm_internal_Property_nativeGetType 535660766160
–> Java_io_realm_internal_Property_nativeGetLinkedObjectName 535660766160
–> Java_io_realm_internal_Property_nativeGetColumnIndex 535660766160
–> Java_io_realm_internal_OsObjectSchemaInfo_nativeGetProperty 535659735680
V/REALM_JNI: --> Java_io_realm_internal_Property_nativeGetColumnIndex 535660767552
–> Java_io_realm_internal_Property_nativeGetType 535660767552
–> Java_io_realm_internal_Property_nativeGetLinkedObjectName 535660767552
–> Java_io_realm_internal_Property_nativeGetColumnIndex 535660767552
–> Java_io_realm_internal_OsObjectSchemaInfo_nativeGetProperty 535659735680
–> Java_io_realm_internal_Property_nativeGetColumnIndex 535660764144
–> Java_io_realm_internal_Property_nativeGetType 535660764144
–> Java_io_realm_internal_Property_nativeGetLinkedObjectName 535660764144
–> Java_io_realm_internal_Property_nativeGetColumnIndex 535660764144
V/REALM_JNI: --> Java_io_realm_internal_OsSharedRealm_nativeIsClosed 534693498880
–> Java_io_realm_internal_OsSharedRealm_nativeIsInTransaction 534693498880
–> Java_io_realm_internal_UncheckedRow_nativeSetFloat 535659853024
–> Java_io_realm_internal_OsSharedRealm_nativeIsClosed 534693498880
–> Java_io_realm_internal_OsSharedRealm_nativeIsInTransaction 534693498880
–> Java_io_realm_internal_UncheckedRow_nativeSetLong 535659853024
–> Java_io_realm_internal_OsSharedRealm_nativeIsClosed 534693498880
–> Java_io_realm_internal_OsSharedRealm_nativeCommitTransaction 534693498880
V/REALM_JNI: --> Java_io_realm_internal_OsSharedRealm_nativeCloseSharedRealm 534693498880
D/REALM_JAVA: last session dropped, remove network listener
D/REALM_JAVA: AuthenticateResponse - Error: IO_EXCEPTION(0)
java.io.InterruptedIOException: thread interrupted
at okio.Timeout.throwIfReached(Timeout.java:145)
at okio.Okio$1.write(Okio.java:76)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:180)
at okio.RealBufferedSink.flush(RealBufferedSink.java:216)
at okhttp3.internal.http1.Http1Codec.finishRequest(Http1Codec.java:164)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:84)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at io.realm.internal.network.OkHttpAuthenticationServer.authenticate(OkHttpAuthenticationServer.java:145)
at io.realm.internal.network.OkHttpAuthenticationServer.loginToRealm(OkHttpAuthenticationServer.java:70)
at io.realm.SyncSession$1.execute(SyncSession.java:541)
at io.realm.SyncSession$1.execute(SyncSession.java:537)
at io.realm.internal.network.ExponentialBackoffTask.run(ExponentialBackoffTask.java:79)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)


#2

My wild guess is that the local Realm instance count reaches 0 because of the try-finally so Realm’s Session is closed as well, which interrupts the sync process to ROS.

You should have at least 1 open Realm on at least 1 thread to keep the session alive. I think the onCreate/onDestroy way of doing things (in Activity) would fix this. If that seems intrusive, you could also register a Application.ActivityLifecycleCallbacks.