Pradeep Bishnoi
Pradeep Bishnoi

Reputation: 1873

Realm Model update from other thread but should notify adapter

I am sending message from adapter to Websocket in websocket onReponse method i am updating Realm Model class but process is going in background and not moving to forward : here you can look my code :

public static void updateMsgStatus(String userId, String messageId, MSG_STATUS msgStatus) {

    Realm realm = Realm.getDefaultInstance();

    Message message = realm.where(Message.class)
            .equalTo(Message.USER_ID, userId)
            .equalTo(Message.MESSAGE_ID, messageId).findFirst();

    realm.beginTransaction();
    message.setMsgStatus(msgStatus.ordinal());
    realm.copyToRealmOrUpdate(message);
    realm.commitTransaction();
}

In this line message.setMsgStatus(msgStatus.ordinal()); after nothing do going in background but i am waiting here :

if i am creating my own thread in this method then realm will not notify adapter Thanks in Advance :)

Upvotes: 3

Views: 800

Answers (2)

Duncan Hoggan
Duncan Hoggan

Reputation: 5100

You could use use a change listener on the realm instance where your data is bound/rendered, Docs here. As I'm sure you would imaging this notifies changes to objects that you have subscribed to.

realm = Realm.getDefaultInstance();
RealmResults<Message> messages = realm.where(Message.class).findAllAsync();

messageChangeListener = new RealmChangeListener() {
    @Override
    public void onChange() {
        // update your dataset
        mAdapter.notifyDataSetChanged();
    }
};
messages.addChangeListener(messageChangeListener);

Upvotes: 2

Bogdan Ustyak
Bogdan Ustyak

Reputation: 5837

What kind of adapter are you using ? If this is the default one you can try just use something like

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mAdapter.notifyItemChanged(positionOfItemToChange);
        }
    });

Upvotes: 0

Related Questions