I'm filtering my database query with NSPredicate
s directly on the database, but then I would like to go further and filter the returned values (Results<T>
) with a custom predicate:
elements.filter { (element) -> Bool in
return ...
}
this one returns a LazyFilterBidirectionalCollection
- how can I use this and get the Results again?
We're tracking adding support for block-based predicates in GitHub issue #2138. This will allow you to perform custom filtering outside of that supported via Realm's built-in primitives.
If you need to sometimes work with a Results<T>
and other times work with a LazyFilterBidirectionalCollection
you can wrap the values in a type-erased wrapper such as AnyBidirectionalCollection<T>
, which forwards any operations to the wrapped type, while hiding the underlying collection.
For instance:
func maybeFilter(results: Results<Foo>) -> AnyBidirectionalCollection<Foo> {
if (condition) {
return AnyBidirectionalCollection(results.filter { $0.foo != "bar" })
}
return AnyBidirectionalCollection(results)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With