действительно борется с чем-то, что, по-моему, должно быть легко.Фильтрация на обратных отношениях с Realm Swift
Использование Swift 3.0, 2.4.2 RealmSwift
У меня есть две модели, тот, который имеет обратную связь с другими с помощью 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")
}
QuestionModels имеют несколько SetModels. У SetModels есть несколько ВопросовModels.
SetModel может иметь имя, например, «Food», «Спорт», «Путешествия» и т.д.
В какой-то момент в моем коде мне нужно найти подмножество QuestionModels, что приравнивать к ряду разные параметры фильтрации, один из которых - 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
}
Edit: Вот ро через question.sets, который показывает, что я могу получить доступ к наборам и есть данные в там
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;
}
);
}
)
Любой помощи очень ценится