Multiple sync agents attempted to join the same session

ros

#1

Hello everyone.

Im currently working in an android application in which i use realm. However i came accross the error in the title.

Im figuring this is because i have an realm object open and executing transactions in a Service, and im opening another one in my Main Activity.

Code of Main Activity:

RealmResults<Object> realmResults =
                mRealm.where(HeartRateMeasurementObject.class)
                        .greaterThanOrEqualTo("timestamp", fromTimestamp)
                        .and()
                        .lessThanOrEqualTo("timestamp", System.currentTimeMillis())
                        .findAll();

        for (Object realmResult : realmResults) {
            returnResult.add(mRealm.copyFromRealm(realmResult));
        }

The Realm object in de Service is created in onCreate and closed in onDestroy. Since the message received function in the Service in another thread i needed to create an handler like so:

 Handler mainHandler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                mRealm.executeTransactionAsync(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.insertOrUpdate(object);
                    }
                });
            }
        };

        mainHandler.post(runnable);

To my knowledge this will execute the code in the main looper, or main thread of the application.

After some research i found that the code in the main activty is also executed on that same thread. Im getting the error when executing the code in the Main Activity at the same time the code in the Service is running.
Both pieces of code run fine, but only seperate and not simultanious.

Any ideas?