Relationships not being added to realm


#1

I have an Owner model of which one of the properties is a relationship to Dog. There is also an ACL property on Owner, and some other basic properties.

func insertOwner(owner: Owner) {
        print(owner.dog?.name ?? "nil")
        guard let realm = realm, let user = user else { return }
        try! realm.write {
            realm.add(owner)
            
            let perm = comment.permissions.findOrCreate(forRoleNamed: "__User:" + user.id)
            perm.canRead = true
            perm.canUpdate = true
            perm.canDelete = true
            
            let everyonePerm = comment.permissions.findOrCreate(forRoleNamed: "everyone")
            everyonePerm.canRead = true
        }
    }

This code prints the owner’s dog’s name, but in realm studio I see no dog on the new owner - it’s nil. Also, the owner says it has two permissions, but when i click it says that “nobody can access this”. What am i doing wrong?


#2

Can you please share the Owner and Dog modells too?


#3
class Owner: Object {
    @objc dynamic var creator: User?
    @objc dynamic var date: Date = Date()
    @objc dynamic var dog: Dog?
    @objc dynamic var text: String? = nil
    @objc dynamic var pic: String? = nil
    @objc dynamic var index: Int = 0
    @objc dynamic var name: String? = nil
    let permissions = List<__Permission>()
}

class Dog: Object {
    @objc dynamic var name: String = ""
    @objc dynamic var collar: Collar?
    @objc dynamic var desc: String? = nil
    @objc dynamic var breed: Int = 0
    @objc dynamic var creator: User?
    @objc dynamic var date: Date = Date()
    let permissions = List<__Permission>()
    let owners = LinkingObjects(fromType: Owner.self, property: "dog")

    override static func primaryKey() -> String? {
        return "name"
    }
}

I also noticed that the “creator” property of owner is added to realm, so my thread title is not totally true. In my model dogs can have multiple owners, but owners have only one dog. My realm is partial sync and owners and dogs are synced by different subscriptions.


#4

I am not sure about that… let me check up about this.


#5

Haha, I changed some names for clarity but probably obfuscated things more, apologies.

Thanks.

Edit: I’m wondering if Realm isn’t adding the dog to the owner because the user does not have permissions on the dog. I’ll test this later.

Edit 2: Yeah, you need read permissions on a dog’s ACL to be allowed to add it to an owner.

The other issue I’m having with permissions not showing up in Studio is probably just a bug in Studio; the permissions are working otherwise.


#6

Yeah, the code suppose to work.
I dont really know anything about permissions (shame on me), im not use them yet :slight_smile: