Multiple sync agents attempted to join the same session



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 =
                        .greaterThanOrEqualTo("timestamp", fromTimestamp)
                        .lessThanOrEqualTo("timestamp", System.currentTimeMillis())

        for (Object realmResult : realmResults) {

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() {
            public void run() {
                mRealm.executeTransactionAsync(new Realm.Transaction() {
                    public void execute(Realm realm) {

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?