Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to keep realmlist updated in the UI

Tags:

android

realm

I have a recyclerview which shows a list of students. All of the students are held in a realmlist in the adapter.

When the user can ask for a data refresh, than the server sends the list of students back to the user. What i am doing now is to download all of the information from the server , store it in the db , than retrieving it from the database(via realmresults) and than converting the realmresult to realmlist.

My question is how to properly update the UI? I have seen in the documentation that realmlist have a managed mode where they are updating the ui automatically.. What is this managed mode? What does it means? How do i use the realmlist to keep it in a managed state? And what is the right way(aka best practice) to use realmlists?

Note that i cannot hold my information as realmresult directly because im performing some manipulation on the data that i dont want it to be saved.

like image 956
Hay Zohar Avatar asked Nov 05 '25 16:11

Hay Zohar


1 Answers

Managed Object vs. Standalone

The standalone RealmObject/RealmList is created through the Object's constructor or the Realm.copyFromRealm() method. The data accessing in the standalone object won't go through the underline storage engine, instead, it behaves just like normal object. So the standalone object won't be refreshed when data changes. Examples for standalone object:

MyModel myModel = new MyModel(); // Standalone

MyModel model = realm.where(MyModel.class).findFirst(); // This is managed object.
MyModel standaloneModel = realm.copyFromRealm(model); // The returned value is standalone object.

MyList myList = new MyList(); // Standalone

The managed RealmObject/RealmList are accessing data though Realm's underlying storage engine. They are created when you do query from Realm, or the return from the copyToRealm() (and its variant methods). Like:

MyModel model = realm.where(MyModel.class).findFirst();

MyModel model = new MyModel(); // This is a standalone object.
model = realm.copyToRealm(modle); // The returned value is managed by Realm now.

MyList myList = realm.where(MyModel.class).findFirst().getMyList();

How to properly update the UI

The suggestion is using Realm's change listeners. See https://realm.io/docs/java/latest/#notifications

And what is the right way(aka best practice) to use RealmList?

This is a bit confusing, but Realm is introducing RealmCollection into the next major release (v0.89.0). See https://github.com/realm/realm-java/pull/2345.

Note that i cannot hold my information as realmresult directly because im performing some manipulation on the data that i dont want it to be saved.

If the RealmList is in managed mode, the data changes will be saved as well. If you don't want some data to be saved, you could consider to use @Ignore annotation on those fields. See https://realm.io/docs/java/latest/#ignoring-properties


Update on 04072016

RealmList vs. RealmResults:

RealmList is a list of RealmObject saved as a field of a RealmObject. It represents the one-to-many relationship in Realm.

RealmResults is the results of query.

Both of them (if RealmList in managed-mode) will be auto-refreshed when data changes.

You can get a RealmResults from a RealmList by RealmList.where() like:

RealmResults results = myRealmList.where().findAll();

This answer will be a bit out-of-date after Realm v0.89.0 released, because of new RealmCollection.

like image 52
beeender Avatar answered Nov 07 '25 11:11

beeender



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!