领域:谓词返回LazyFilterCollection-如何转换为Results< T&gt ;? [英] Realm: predicate returning LazyFilterCollection - how to convert to Results<T>?

查看:52
本文介绍了领域:谓词返回LazyFilterCollection-如何转换为Results< T&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要直接在数据库上用NSPredicate过滤数据库查询,但是我想进一步使用自定义谓词过滤返回的值(Results<T>):

I'm filtering my database query with NSPredicates 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&lt; T&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆