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?
source to share
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.
source to share
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.
source to share