swift - 使用 Realm Swift 过滤逆关系

标签 swift filter realm predicate

我确实在为一些我认为应该很容易的事情而苦苦挣扎。

使用 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/

相关文章:

filter - 在表格中排列过滤器值

ios - 获取包含在 RLMArray 中的类

swift - base64 编码的 PDF 无法在 swift 中解码

matlab - MATLAB 中的滤色器

java - 启动过滤器时出现异常 - Servlet

android - Image Url 存储在相机和图库 Intent 的 Realm 中,但在 ListView 中看不到

swift - 基于子对象列表中的值的 Realm 过滤结果

ios - 使用 Swift 将常用方法放在单独的文件中

ios - 我在 swift 中面临一个问题 tabBar 菜单和侧边栏菜单

Swift - 无法专门化非通用定义