Let's say that we have a group
object with a field called contacts
on it. Contacts is a list that holds references to contact objects and can in theory have up to 20 000 objects in it.
My question(s):
If I query group
object, am I taking a performance hit because there's a huge list field on it? If not, am I taking a performance hit when I access that field?
If this design is not good, would it be better to instead have a groups
field on contact
object, since there is probably a lot less groups than there is contacts?
Thanks in advance!
Non-code questions are challenging as we don't know the entire use case but let me address this at a high level.
Generally speaking, Realm Results which are Collections are lazily-evaluated results of a query operation. A realm List is also an example of a Collection.
This lazy evaluation enables code to be written that is elegant, high performance code even when the data sets are gigantic and with complex queries.
You can generally work with Collections like a regular JavaScript array but they don't actually hold matching Realm objects in memory. Instead they reference the matched objects stored in the Realm file.
The end result is that large datasets have very small memory footprints which in most cases avoids the need for paginating the query as the Realm objects are only loaded when used. Pagination can then be handled in memory on the device by just displaying smaller number of elements from the query, appropriate for your UI
e.g. if the query returns 10,000 results, optionally use pagination to display 10 at a time via an index [0...9] and then [10...19] for example. That technique preserves the query results but also protects memory as only those 10 elements are loaded but you still have access to the entire query results
So, as long as you keep your Realm objects as a Collection type - then no, the memory impact is minimal. However, and this is the important bit, as soon as high level functions are run against those collections or they are mapped to an array, ALL of the data is loaded.
Best practice is to keep Realm objects 'Realmy' by leveraging Collections (Results, Lists etc).
If your Contacts is a List collection then you should be good to go.