2

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?

Volo Apps
  • 235
  • 1
  • 3
  • 12
  • Just create your realm instance in the other thread doing the async transaction and you're good to go. – Anis LOUNIS aka AnixPasBesoin Jan 31 '17 at 23:31
  • 1
    why not use `executeTransactionAsync()`? – EpicPandaForce Feb 01 '17 at 07:33
  • `•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.` please read the documentation and how `RealmChangeListener` works, https://realm.io/docs/java/latest/#notifications there is typically zero need to access any managed Realm* classes off the thread where they are – EpicPandaForce Feb 01 '17 at 07:35
  • @anixPasBesoin ok will do it on the background thread with the instance of the Realm and see if it works,if works you shall put that as an aswer – Volo Apps Feb 01 '17 at 07:49
  • @AnixPasBesoin post it as an aswer it worked! – Volo Apps Feb 01 '17 at 08:25
  • Related to http://stackoverflow.com/questions/39382021/what-is-the-most-efficient-way-to-store-long-list-of-objects-in-realm – EpicPandaForce Feb 01 '17 at 12:12

1 Answers1

-1

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