Deveti Putnik Deveti Putnik - 1 year ago 46
Android Question

How does maxDepth parameter in copyFromRealm method affects performance?

I have a object A which contains list of objects of type B which contains list of objects of type C. This is the deepest level of hierarchy. Now, when I do where() query I need to have all these data as a result (meaning, objects A which contains objects B, which contains objects C ). The "problem" is that I need a list of unmanaged objects.

So, my question is: if I want to fetch all these data, are there any differences in fetching speed when I provide

parameter or not to
method, since I need this for the deepest possible depth in hierarchy?

Answer Source

When you set maxDepth value, Realm will provide null values beyond the given maxDepth level. So, if you are fine with null values, setting maxDepth will be faster, because Realm sets null and not the actual value. In case you are not OK with null value, and set the maxDepth as the maximum possible depth as per your code, it will have same performance.

Performance wise there is no difference; if you use copyFromRealm with or without maxDepth parameter, because in case you don't provide a maxDepth value, Realm sets it as Integer.MAX_VALUE and calls copyFromRealm method like following,

public <E extends RealmModel> List<E> copyFromRealm(Iterable<E> realmObjects) {
    return copyFromRealm(realmObjects, Integer.MAX_VALUE);

So, in both the cases, you call the same function.

TL;DR - If your maxDepth is 3(A->B->C) and you set maxDepth as 3, there is no performance difference, if you set maxDepth as 0 < maxDepth < 3 then yes its faster, but you get null values beyond the maxDepth.