Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Realm, should I use a List object or Results object as the data source for a UITableView?

There are at least 2 main collection types used in Realm:

  1. List
  2. Results

The relevant description from the documentation on a Results object says:

Results is an auto-updating container type in Realm returned from object queries.

Because I want my UITableView to respond to any changes on the Realm Object Server, I really think I want my UITableView to be backed by a Results object. In fact, I think I would always want a Results object to back my UI for this reason. This is only reinforced by the description of a List object in the documentation:

List is the container type in Realm used to define to-many relationships.

Sure seems like a List is focused on data modeling... So, being new to Realm and just reading the API, I'm thinking the answer is to use the Results object, but the tutorial (Step 5) uses the List object while the RealmExamples sample code uses Results.

What am I missing? Should I be using List objects to back my UITableViews? If so, what are the reasons?

like image 662
David Weiss Avatar asked Oct 18 '16 05:10

David Weiss


2 Answers

Short answer: use a List if one already exists that closely matches what you want to display in your table view, otherwise use a Results.

If the data represented by a List that's already stored in your Realm corresponds to what you want to display in your table view, you should certainly use that to back it. Lists have an interesting property in that they are implicitly ordered, which can sometimes be helpful, like in the tutorial you linked to above, where a user can reorder tasks.

Results contain the results of a query in Realm. Running this query typically has a higher runtime overhead than accessing a List, by how much depends on the complexity of the query and the number of items in the Realm.

That being said, mutating a List has performance implications too since it's writing to the file in an atomic fashion. So if this is something that will be changing frequently, a Results is likely a better fit.

like image 61
jpsim Avatar answered Nov 14 '22 23:11

jpsim


You should use Results<> as the Results is auto updating to back your UITableView. List can be used to link child models in a Realm model. where as Results is used to query the Realm Objects and you should add a Realm Notification Token so you know when the Results are updated and take necessary action (reload table view etc.) Look here for realm notifications: https://realm.io/docs/swift/latest/#notifications

P.S. The data in that example is just static and no changes are observed

like image 41
Praveen Pendyala Avatar answered Nov 14 '22 23:11

Praveen Pendyala