Using Realm in loop or To Insert List Android

Good day. I have an issue with realm.

First of all it seems to me a good databse to use for local data to be stored and i have developed a social network where i save messages locally and on every application start i am downloading the messages from server to save them locally for let’s say current session to be used. Realm seemed to be the best solution out of box but yet i encounter not good situation where my UI is blocked due to Realm transaction. Here are the cases.

• I have to use the Realm object from the UI thread,otherwise it will throw exception saying that i need to access the object from the thread i have called so in this situation the executeTransactionAsync from the realm will not work as still the transaction will actually happen on the UI thread

•This is first time such issue,but yet i imagine myself why it was not an issue before,as my mesasges was not so much and now i have like 40 messages and wow…realm blocking UI really horrible (just about 4 seconds) which is pathethic.

•I have tried to run the realm actual transaction and the method i have called in a separate thread but that will not work ofcourse as at the end the Realm object must be accessed from the UI thread,so no luck with background Threads at all.

So i came here to ask for an help if anyone can please?

Here is a code of realm method

 public void insertMessage(final String userId, final String opponentId, final String message,
                          final String messageId, final String date,
                          final String id, final String deliveryStatus,
                          final String userImage,
                          final String opponentImage, final String deleteOpponentId,
                          final String deleteUserId,
                          final boolean hasGif, final String gifUrl, final boolean animated) {

    if (!isMessageExist(messageId)) {
        handler.post(new Runnable() {
            @Override
            public void run() {

            }
        });
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                MessageModel messageModel = realm.createObject(MessageModel.class);

                messageModel.setId(id);
                messageModel.setMessageId(messageId);
                messageModel.setMessage(message);
                messageModel.setUserId(userId);
                messageModel.setOpponentId(opponentId);
                messageModel.setAnimated(animated);
                messageModel.setDeliveryStatus(deliveryStatus);
                messageModel.setUserImage(userImage);
                messageModel.setOpponentImage(opponentImage);
                messageModel.setDate(date);
                messageModel.setHasGif(hasGif);
                messageModel.setGifUrl(gifUrl);
                messageModel.setDeleteUserId(deleteUserId);
                messageModel.setDeleteOpponentId(deleteOpponentId);
            }
        });
    }


}

The handle is the MainLooper basically the UI thread at this moment

and i call this method inside the loop where i get the messages array,that’s it,any help please?

Answer

Just create your realm instance in the other thread doing the asynchronous transaction and you’re good to go.

Useful samples

Realm android thread example

Related question

Realm access from incorrect thread

Leave a Reply

Your email address will not be published. Required fields are marked *