Realm: predicate returning LazyFilterCollection - how to convert to Results<T>?

倖福魔咒の 提交于 2019-12-12 10:44:39

问题


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 ... 
}

this one returns a LazyFilterBidirectionalCollection - how can I use this and get the Results again?


回答1:


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)
}


来源:https://stackoverflow.com/questions/42912998/realm-predicate-returning-lazyfiltercollection-how-to-convert-to-resultst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!