我已将代码从 CoreData 移植到 Realm,除了最后 1 个问题外,一切都运行良好。
使用 RealmOptional 时的过滤是删除所有值为 nil 的对象。
例如,.filter("price <= 10.0")
正在从结果集中删除价格为零的所有对象。使用 NSFetchedResultsController 和 NSPredicates 时,这种行为在 CoreData 中没有发生,所以我想知道这是否是 Realm 的预期行为?
对象如下 RealmOptional<Double>
在下面的示例中:
class Product : Object, Mapper
{
var price = RealmOptional<Double>() {
// Using ObjectMapper library to map JSON to Realm hence willSet
willSet {
self.price = newValue
}
}
}
我希望结果返回所有 Products
价格 < 10.0,包括零值。
这是预期的行为还是仅仅是一个错误?
最佳答案
如果您按数字比较运算符过滤,则不包括具有空值的对象是预期的行为。如果您想包含 price
为 nil
的对象,您可以添加 OR price = nil
。像下面这样:
let free_or_cheap_products = realm.objects(Product)
.filter("product <= 10 || product = nil")
关于ios - 在 RealmOptional 上过滤会删除 nil 值,而 CoreData 谓词不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896141/