Realm.objects () filter not working with Resuts <T>

I tried to execute query objects from Realm using RealmSwift framefowk

with predicates:

realm.objects(Train).filter("ANY route IN %@", realm.objects(Route))

      

Mistake:

Cannot call "filter" using argument list of type (String, Results)

The same code worked with Realm framework

, but doesn't work with RealmSwift framework

.

What am I doing wrong?

+3


source to share


3 answers


The problem is that it Results.filter(_:...)

takes type arguments (String, CVarArgType...)

but Results

doesn't match CVarArgType

. I just created a GitHub issue # 1995 to keep track of this. The fix is ​​pretty simple and I hope there will be a PR coming out soon to fix this.

At the same time you can convert the arguments Results

and List

in Results.filter(_:...)

in Array

using map(_:_:)

the following way:



realm.objects(Train).filter("ANY route IN %@", map(realm.objects(Route)) { $0 })

      

EDIT: PR # 1996 , when Results

matched CVarArgType

, is now pending review.

+1


source


The results are not counted as an array in your case, you can do it simply with this piece of code. Have you tried matching realm.objects(Route)

to return an array of IDs and filter something like this on it ANY route.yourIdentifier IN %@

:?



0


source


I wrote this extension

extension Results {
   func map<U>(transform: (T) -> U) -> Results<U> {
       return Results<U>(Swift.map(self, transform))
   }
}

      

but mistake

'Results<U>' cannot be constructed because it has no accessible initializers

      

maybe because the results cannot be generated directly like in the Realm docs.

0


source







All Articles