我确实在为一些我认为应该很容易的事情而苦苦挣扎。
使用 Swift 3.0、RealmSwift 2.4.2
我有两个模型,其中一个使用 LinkingObjects 与另一个模型具有反比关系。
class SetModel: Object {
dynamic var name = ""
let questions = List<QuestionModel>()
}
class QuestionModel: Object {
dynamic var level = ""
let sets = LinkingObjects(fromType: QuestionModel.self, property: "questions")
}
QuestionModel 有多个 SetModel。 SetModels 有多个 QuestionsModels。
SetModel 可以具有“食物”、“运动”、“旅行”等名称。
在我的代码中的某个时刻,我需要找到 QuestionModel 的子集,它相当于许多不同的过滤选项,其中之一是 Sets。 即获取某些集合中所有“简单”的问题。
我获得了多个集合,例如“食物”和“旅行”,并且需要查找在“集合”属性中设置了该集合的所有问题。
fileprivate func applyFilters(objects: Results<QuestionModel>) -> Results<QuestionMode> {
var res = objects
// I have a session attribute that stores the values I need to filter on,
// one of which is a List<SetModels>
if (session.sets.count > 0) {
// These are the things I've tried, and the error messages :(
for s in session.sets {
// *** Terminating app due to uncaught exception 'Invalid predicate',
// reason: 'Key paths that include an array property must use
// aggregate operations'
res = res.filter("sets.name == \(s.name!)")
}
for s in session.sets {
// *** Terminating app due to uncaught exception 'Invalid predicate',
// reason: 'Predicate with ANY modifier must compare a KeyPath with
// RLMArray with a value'
res = res.filter("ANY sets.name == \(s.name!)")
}
for s in session.sets {
// *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
// reason: 'Unable to parse the format string "ANY sets == SetModel
res = res.filter("ANY sets == \(s)")
}
// *** Terminating app due to uncaught exception 'Invalid predicate',
// reason: 'Key paths that include an array property must use aggregate
// operations'
res = res.filter("sets.name IN \({'Food', 'Travel'})")
}
// Filter on level
res = res.filter("level == 'easy'")
return res
}
编辑:这是一个question.sets的po,它显示我可以访问集合并且其中有数据
po self.questionModel.sets
LinkingObjects<SetModel> (
[0] SetModel {
uuid = 5899cb786f6986.79052132;
name = Action;
questions = RLMArray <0x7f9a529a6910> (
[0] QuestionModel {
question = What can you climb?;
responses = Tree;
},
[1] QuestionModel {
question = How could you get into town?;
responses = Bus;
}
);
},
[1] SetModel {
uuid = 5899cbc2938b87.61048461;
name = Travel;
questions = RLMArray <0x7f9a52c05d60> (
[0] QuestionModel {
question = How could you get into town?;
responses = Bus;
}
);
}
)
非常感谢任何帮助
最佳答案
嗯,一天后我已经弄清楚了......
let predicate = NSPredicate(format:"SUBQUERY(sets, $s, $s.name IN %@).@count > 0", ['Demo', 'Food'] )
res = res.filter(predicate)
老实说,有时我感觉自己就像一个魔术师学徒,只是随意念咒语:)
关于swift - 使用 Realm Swift 过滤逆关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114225/