Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 407
Pradeep Bishnoi Avatar asked Dec 17 '25 16:12

Pradeep Bishnoi


1 Answers

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);
like image 198
Duncan Hoggan Avatar answered Dec 19 '25 05:12

Duncan Hoggan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!