Realm-cocoa: Adding 2-level to-many relationship items

Created on 13 Feb 2017  路  2Comments  路  Source: realm/realm-cocoa

Goals

Write a object ("Parents") which have multiple items ("Children") which have multiple items ("Dogs") in one transaction. The link is s follows
Parents <-> Children -> Dog

Expected Results

RealmParents {
    name = Parent 1;
    children = RLMArray <0x1700f7d80> (
        [0] RealmChildren {
            name = Children 1;
            dogs = RLMArray <0x1700f8100> (
                [0] RealmDog {
                    name = Dog 1;
                }
            );
        },
        [1] RealmChildren {
            name = Children 2;
            dogs = RLMArray <0x1700f8100> (
                [0] RealmDog {
                    name = Dog 2;
                }
            );
        }
    );
}

Actual Results

RealmParents {
    name = Parent 1;
    children = RLMArray <0x1700f7d80> (
        [0] RealmChildren {
            name = Children 1;
            dogs = RLMArray <0x1700f8100> (
                [0] RealmDog {
                    name = Dog 2;
                }
            );
        },
        [1] RealmChildren {
            name = Children 2;
            dogs = RLMArray <0x1700f8100> (
                [0] RealmDog {
                    name = ;
                }
            );
        }
    );
}

(see the dog items)

Steps to Reproduce / Code Sample

The objects are defined as follows:

class RealmDog: Object {
    dynamic var name = ""
    override static func primaryKey() -> String? {
        return "name"
    }
}
class RealmChildren: Object {
    dynamic var name = ""
    let dogs = List<RealmDog>()
    let parent   = LinkingObjects(fromType: RealmParents.self, property: "children")
    override static func primaryKey() -> String? {
        return "name"
    }
}
class RealmParents: Object {
    dynamic var name = ""
    let children = List<RealmChildren>()
    override static func primaryKey() -> String? {
        return "name"
    }
}

let dog1 = RealmDog()
                dog1.name = "Dog 1"
                let dog2 = RealmDog()
                dog1.name = "Dog 2"

                let children1 = RealmChildren()
                children1.name = "Children 1"
                children1.dogs.append(dog1)

                let children2 = RealmChildren()
                children2.name = "Children 2"
                children2.dogs.append(dog2)

                let parent1 = RealmParents()
                parent1.name = "Parent 1"
                parent1.children.append(children1)
                parent1.children.append(children2)
                let realm = try! Realm()
                do {
                    try realm.write {
                        realm.add(parent1, update: true)
                    }
                } catch let error as NSError {
                    print(error)
                }
                print(realm.object(ofType: RealmParents.self, forPrimaryKey: "Parent 1"))

Version of Realm and Tooling

In the CONTRIBUTING guidelines, you will find a script,
which will help determining these versions.

```
Xcode 8.2.1
Realm version: realm swift 2.4.2
iOS/OSX version: 10.2.1

Am I missing something? Happy for any help or tips! Thanks, guys for your work!

T-Help

Most helpful comment

Hi @bdash ,

thank you very much for your help! Yes, it was my fault. I have a complex structure to create a Realm object and even after checking the code several times I haven't found anything. Then I wrote quickly this wrong snippet and the result confirmed me that I didn't do anything wrong. What a mistake! Sorry for that! I've found my bug and it's working as expected. Thank you very much!

All 2 comments

I think you have a copy-paste bug in your code. Note the duplicate assignment to dog1.name:

let dog1 = RealmDog()
dog1.name = "Dog 1"
let dog2 = RealmDog()
dog1.name = "Dog 2"

Hi @bdash ,

thank you very much for your help! Yes, it was my fault. I have a complex structure to create a Realm object and even after checking the code several times I haven't found anything. Then I wrote quickly this wrong snippet and the result confirmed me that I didn't do anything wrong. What a mistake! Sorry for that! I've found my bug and it's working as expected. Thank you very much!

Was this page helpful?
0 / 5 - 0 ratings