0

I have a RealmResults object which i am updating but the problem is that it does not get updated in real time.

code...

Thread syncMessages = new Thread(() -> {
        final Realm realm = Realm.getDefaultInstance();
        ChatManager chatManager = new ChatManager(realm, context);
            resultMessages = chatManager.sendPendingMessages(user.getId_user());
        countDownLatch.countDown();
        realm.close();
    });

 try {
        syncMessages.start();
        countDownLatch.await();
    } catch (Exception e) {
        e.printStackTrace();
    }

sendPendingMessages is the function that contains the realmresult object.

RealmResults<ChatMessage> messages = chatRepository.pendingSyncMessages();
//it fetches all chat messages that are not synced.

I create a service to sync data with server and since its on a different thread,i need to open another realm instance to update the data locally so it knows that the data has been synced

OnSuccessCall of the Service

Realm innerRealm = Realm.getDefaultInstance();
ChatRepository innerchatRepository = new ChatRepository(innerRealm);                                        
innerchatRepository.updateSyncForChatMessage();
RealmResults<ChatMessage> messages_middle = innerchatRepository.pendingSyncMessages();

Within the on success of the service the realmresults object reflects the changes correctly so the size of messages_middle is less than messages since data was synced but when i check the size of messages outside the service after it has synced data,it still shows the original size and the original data.

Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
  • Have you considered using RealmChangeListener instead of latches? – EpicPandaForce Jul 22 '17 at 22:49
  • @EpicPandaForce the latches are not related to realm as such,they are there so i can process this asynchronous code in a synchronous manner.Can i use a RealmChangeListener on a thread like this one?I am not sure if it has a looper of its own. – Jude Fernandes Jul 23 '17 at 05:46
  • `realm.refresh()` can update non-looper background thread (added in 3.2.0) – EpicPandaForce Jul 23 '17 at 08:31

0 Answers0