我有一个DeviceContactModel
和一个DeviceContactPhoneModel
继承自Object
(领域)。DeviceContactModel
有一个List
<DeviceContactPhoneModel>
。我想通过DeviceContactModel
属性之一筛选DeviceContactPhoneModel
我做了测试代码,但它称为应用程序崩溃。请告诉我如何实施?谢谢。
class DeviceContactModel: Object, Mappable {
@objc dynamic var id = ""
@objc dynamic var givenName = ""
@objc dynamic var familyName = ""
@objc dynamic var updateTimestamp = 0.0
var isNew = false
let phones = List<DeviceContactPhoneModel>()
}
final class DeviceContactPhoneModel: Object, Mappable {
@objc dynamic var id = ""
@objc dynamic var contactID = ""
@objc dynamic var updateTimestamp = 0.0
@objc dynamic var countryCode: Int64 = 0
@objc dynamic var nationalNumber: Int64 = 0
@objc dynamic var fullNumber: Int64 = 0
}
测试函数
private func getDeviceContacts(_ phoneNumbers: [Int64]) -> [DeviceContactModel] {
do {
let realm = try Realm()
let deviceContacts = Array(realm.objects(DeviceContactModel.self).filter("phones.fullNumber IN %@", phoneNumbers))
return deviceContacts
} catch {
debugPrint(error.localizedDescription)
return []
}
}
故障日志
谓词无效,reason:'包含数组属性的键路径必须使用聚合操作
最佳答案
试试这个办法
如果使用多对多关系,则使用ANY
运算符
请仔细阅读这篇文章,以便您理解使用什么,因为还有另一个操作员exALL,ANY,NONE
Predicate Programming Guide
private func getDeviceContacts(_ phoneNumbers: [Int64]) -> [DeviceContactModel] {
do {
let realm = try Realm()
let deviceContacts = Array(realm.objects(DeviceContactModel.self).filter("ANY phones.fullNumber IN %@", phoneNumbers))
return deviceContacts
} catch {
debugPrint(error.localizedDescription)
return []
}
}
关于ios - 如何按列表过滤Realm对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49925537/