Skip to content Skip to sidebar Skip to footer

Realm Access From Incorrect Thread

I have an application with a LoginActivity, that when the user login correctly, I register to receive messages. And the LoginActivity jumps to MainActivity. The arriving messages a

Solution 1:

Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

This error message is quite self-explanatory.

As i see you're initializing realm by calling Realm.getDefaultInstance() on the UI thread.

The error is coming from newMessageReceived(), so i guess that method is called from a background thread.

Either obtain a Realm instance on the background thread and use that instead of the global instance:

@Overridepublicvoidrun() {
    RealmbackgroundRealm= Realm.getDefaultInstance();
    backgroundRealm.executeTransactionAsync(newRealm.Transaction() {
        @Overridepublicvoidexecute(Realm realm) {
            MessagereceivedMessage= realm.createObject(Message.class, message.id);
            receivedMessage.setBodyMessage(message.message);
            receivedMessage.setFrom(message.from);
            receivedMessage.setTo(message.to);
            receivedMessage.setDelivered(false);
            receivedMessage.setMine(false);
            receivedMessage.setDate(Calendar.getInstance().getTime());
        }
    });
}

Or, if you would like to stick to the global Realm instance for some reason, then make sure your code is executed on the UI thread by calling runOnUiThread() (or directly posting a Runnable to the message queue of the main thread through a Handler):

@OverridepublicvoidnewMessageReceived(final ChatMessage message) {
    runOnUiThread(newRunnable() {
        @Overridepublicvoidrun() {
            realm.executeTransactionAsync(newRealm.Transaction() {
                @Overridepublicvoidexecute(Realm realm) {
                    Message receivedMessage = realm.createObject(Message.class,
                        message.id);
                    receivedMessage.setBodyMessage(message.message);
                    receivedMessage.setFrom(message.from);
                    receivedMessage.setTo(message.to);
                    receivedMessage.setDelivered(false);
                    receivedMessage.setMine(false);
                    receivedMessage.setDate(Calendar.getInstance().getTime());
                }
            });
        }
    });
}

Solution 2:

Just create Realm backgroundRealm = Realm.getDefaultInstance() each time you want to access database and don't forget to close it using realm.close()

Solution 3:

Allocate instance before transaction and release it right after transaction is complete, so you won't have linegring connection and by doing so, you perform savings from thread scheduler. I use 'Data Access Object' interface for manipulations with data and that interface is implemented using Realm. Don't use 'transaction async', use all calls synchronously, but perform calls on 'data access object' from background thread. Good solution for that - rxJava library. Just get and release Realm instance all the time - library makes it inexpensive.

Solution 4:

I haven't use Realm yet. But what i understood from the error message is that ConnectionConnectResponse may be alive when Loginactivity die. So you should pass Realm instance as a parameter inside

newMessageReceived(Realm realm, final ChatMessage message)

and put all the Realm init code in the class where you fire this method.

Post a Comment for "Realm Access From Incorrect Thread"