领域:谓词返回LazyFilterCollection-如何转换为Results< T> ;? [英] Realm: predicate returning LazyFilterCollection - how to convert to Results<T>?
问题描述
我要直接在数据库上用NSPredicate
过滤数据库查询,但是我想进一步使用自定义谓词过滤返回的值(Results<T>
):
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 ...
}
这个返回一个LazyFilterBidirectionalCollection
-我该如何使用它并再次获得结果?
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.
如果有时需要使用Results<T>
,而有时需要使用LazyFilterBidirectionalCollection
,则可以将值包装在类型擦除的包装器中,例如AnyBidirectionalCollection<T>
,该包装器将所有操作转发到包装的类型,而隐藏基础集合.
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.
例如:
func maybeFilter(results: Results<Foo>) -> AnyBidirectionalCollection<Foo> {
if (condition) {
return AnyBidirectionalCollection(results.filter { $0.foo != "bar" })
}
return AnyBidirectionalCollection(results)
}
这篇关于领域:谓词返回LazyFilterCollection-如何转换为Results< T> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!