ios - Swift 中 Realm 查询的交叉引用谓词

标签 ios swift realm nspredicate

所以我最近一直在学习 Swift 中的 Realm,我遇到了一个关于数据模型之间潜在通知的设计问题。如果有人愿意分享一些见解,我们将不胜感激。

// Class A is what users mainly interact with
Class A: Object {
    dynamic var propertyA = ""
    dynamic var propertyB = ""
    dynamic var propertyC = ""

    override static func primaryKey() -> String? {
        return "propertyA"
    }
}

// Class B is what the server interacts with, like data feeding
Class B: Object {
    dynamic var propertyA = ""
    dynamic var propertyX = ""
    dynamic var propertyY = ""
    dynamic var propertyZ = ""

    override static func primaryKey() -> String? {
        return "propertyA"
    }
}


我在 Controller 中为 Object A 设置了一个通知 block 。因此,只要 resultObjectsA 发生变化,通知 block 就会被触发。

let resultObjectsA = realm.objects(A).filter('predicate1')
let notificationTokenA = resultObjectsA.addNotificationBlock {
    ...
}


在我正在构建的应用程序中,用户可以选择一组对象A,应用程序将从服务器查询选择集的状态。返回结果将存储/呈现在 Object B 中。现在,我需要为 Object B 编写查询/谓词:

let resultObjectsB = realm.objects(B).filter('predicate2')

这样 resultObjectB 条目中的 propertyA 可以在 resultObjectsA 条目的 propertyA 中找到,就像一个 -一对一映射。

我不确定这样的谓词是否可行。如果还有其他方法可以实现我想在这里做的事情,我也非常渴望学习。

最佳答案

要查询所有(实际上应该有一个对象,因为主键)对象 B where objectB.propertyA == objectA.propertyA 使用:

let resultObjectsB = realm.objects(B).filter('propertyA = %@', objectA.propertyA)

另一种选择是使用 AB 之间的一对一关系,了解更多信息 https://realm.io/docs/swift/latest/#to-one-relationships

关于ios - Swift 中 Realm 查询的交叉引用谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956605/

相关文章:

ios - 表示 for 循环内的网络调用已完成,然后转到 for 循环中的下一项?

ios - 将数据传递到tableViewController时出错

ios - 禁用在屏幕特定区域的 uipageviewcontroller 上滑动

ios - 如何使用Unity在iOS模拟器中获取物理屏幕尺寸?

ios - 当我更改 iPhone 的语言时,会导致崩溃吗?

swift - UiSearchController 到 Swift 中的 UiCollectionView

android - RxJava Observable 从不执行 onCompleted

swift - 模型中未定义主键,但显示主键错误

c# - "Invalid Swift Support"在 Xamarin.iOS 中使用 native Swift 库

html - 在 Safari Mobile 10.3 上,粘性页脚可以滚出屏幕