Search code examples
swiftrealmswift4realm-mobile-platform

Nested Queries in Realm. Swift


I am trying to create nested queries in realm. I will paste my models in and explain what I mean.

Parent Model

@objcMembers class Group: Object {

    dynamic var uuid: String = ""
    dynamic var admin: User?

    convenience init(uuid: String, admin: User) {
        self.init()
        self.uuid = uuid
        self.admin = admin
    }
}

Child Model

@objcMembers class Message: Object {

    dynamic var uuid: String = ""
    dynamic var group: Group?
    dynamic var message: String = ""

    convenience init(uuid: String, group: Group, from: User, message: String) {
        self.init()
        self.uuid = uuid
        self.group = group
        self.message = message
    }
}

What I am trying to do is filter messages that are in a group with uuid x

All the answers I have seen are outdated.

What I have right now is

let result = RealmService.shared.realm.objects(Message.self).filter("group.uuid = 0E81CDEF-B63F-4DBE-9900-B486D40F4EC9")

What is the correct way of doing this?


Solution

  • Figured it out:

    let result = RealmService.shared.realm.objects(Message.self).filter("group.uuid = '2C5E1738-1167-40CB-BE43-C415FD5E6E5D'")
    

    Queried value has to be wrapped in ''