realm-java: RealmResults not updating real time after deletion
After running the following code I would have expected the realmList AND realmResults to be the same size after deleting a Realm object contained by both.
RealmList<Notification> realmList = getNotifications();
if (realmList != null && !realmList.isEmpty())
{
RealmQuery<Notification> query = realmList.where()
.notEqualTo("notificationType", Notification.TYPE_BOARD_MESSAGE)
.notEqualTo("notificationType", Notification.TYPE_DIRECT_MESSAGE);
RealmResults<Notification> realmResults = query.findAllSorted(Notification.PARAM_CREATED_AT, Sort.DESCENDING);
Log.d("REALM_TEST", "realmList before has " + realmList.size() + " items");
Log.d("REALM_TEST", "realmResults before has " + realmResults.size() + " items");
Notification firstNotification = realmList.first();
realm.beginTransaction();
firstNotification.deleteFromRealm();
realm.commitTransaction();
Log.d("REALM_TEST", "realmList after has " + realmList.size() + " items");
Log.d("REALM_TEST", "realmResults after has " + realmResults.size() + " items");
}
Output D/REALM_TEST: realmList before has 5 items D/REALM_TEST: realmResults before has 5 items D/REALM_TEST: realmList after has 4 items D/REALM_TEST: realmResults after has 5 items
The realmList updates correctly, removing the object real time. The realmResults however changes the object to “Invalid object” but does not remove it from the list. This results in unexpected behavior, especially when looping over the results.
Is there a better way to achieve this?
Thanks in advance for any help on this.
Version of Realm and tooling
Realm version: 0.90.1
Android Studio version: 2.1.1
Android version: Android 6.0
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (10 by maintainers)
After spending way too much time on this, I think it’s best for me to go back to version 0.88.3. I have way too many RealmResults that need real time updating. Stable iterators are of no interest to me. I was using the
for (int i = results.size() - 1; i >= 0; i--)
technique for removals and everything was fine. One of the main things I love about Realm is the auto updating RealmList and RealmResults. It is a shame to see them go away 😦Maybe in a future release you can introduce a new class (RealTimeRealmResults or AutoRefreshRealmResults?) for people like me.
Sorry I only reopened this in the interest that it might get moved to a feature request 😃
BTW we’re back to the behavior of 0.88.3 in this terns of behavior with 3.1.2