I am having an issue when trying to convert a List<T>
into Results<T>
using RealmSwift
. I have a function that runs a set of results through multiple filters and returns the filtered results. One step in this process, however, requires some calculation and cannot be accomplished by applying an NSPredicate
filter. I, thus, create a list and add the results to the list that meet the criteria. Since I need to return a set of results, I then apply a "TRUEPREDICATE" filter to the list. However, I end up getting a crash with the error message, "This method may only be called on RLMArray
instances retrieved from an RLMRealm
."
I have seen this problem occur in lists of objects that have not yet been added to a Realm
, but these are the results of a previous query, and so have definitely already been saved to the database.
My code looks something like this:
var results:Results<Object> = resultsOfSomeQuery
let list = List<Object>()
for result in results {
if result.matchesSomeCriteria {
list.append(result)
}
}
results = list.filter("TRUEPREDICATE")
If anyone could clarify what is going on here it would be greatly appreciated.
What you are trying to achieve is not possible.
Since Results
is auto-updating, it can only contain managed objects from Realm
. This is why Realm's filter
can also only be called on managed collections. You cannot use Realm's filter
on a List
that you created manually without persisting it in Realm.
If the filtering you need to do really cannot be done using NSPredicate
s, then you need to resort to using non-updating collections.