ios - 在 RealmOptional 上过滤会删除 nil 值,而 CoreData 谓词不会

标签 ios swift core-data realm

我已将代码从 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,包括零值。

这是预期的行为还是仅仅是一个错误?

最佳答案

如果您按数字比较运算符过滤,则不包括具有空值的对象是预期的行为。如果您想包含 pricenil 的对象,您可以添加 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/

相关文章:

ios - AFHTTPRequestOperationManager 获取请求失败,Error Domain=AFNetworkingErrorDomain Code=-1016

ios - 自定义关系续集

ios - en 系统区域设置中 ru 语言的 strigsdict 文件的错误值

ios - 核心数据 : object with minimum difference between property and variable

iphone - CoreData 无法解决从 xml feed 检索数据并使用托管对象进行操作的错误

ios - iOS 7 中 UISearchBar 中的 UITextField

ios - 调度加载 UIImages 产生意外结果

ios - 在 WKWebview 中的 Safari 中打开新选项卡

ios - 核心文本 : Simplest accurate means of getting text bounds?

iPhone核心数据内部不一致