Parent not seeming to be available for appending


Using the analogy from this site’s docs, I want to keep adding ‘Dogs’ to an ‘Owner’. ( Children to Parent ). I know it can be done but there is no explicit example on this site. freeubi’s reply below is from an earlier edit referencing Parents and Children.

All the tutorials and you tubes on the web use To-Do examples with UITableView, even the one from Udemy that introduced me to Realm uses a didSet observer to carry the Owner ( parent ) across the segue into the second TableView for adding the details. There is a guy in various places asking how to match train sets to children, but I’m going loco understanding his question.

My use case does not involve TableViews. All input is gained from a single controller, where different input textFields are sent to Owner or Dog objects.

More after the jump:

    let realm = try! Realm()  
    var owners: Results<Owner>?

 @IBAction func addButtonPressed(_ sender: CustomButton) {

        let dogOwned = ownerField?.text ?? "-"
           // 1.
           if !ownerSet.contains(dogOwned) {
			// 2a.
            let newOwner = Owner()
            newOwner.kind = ownerField?.text ?? "-"
            save(owner: newOwner)
            populate(owner: newOwner)
           } else {
			// 2b.
            let theOwner = Owner()
            populate(owner: theOwner)
    func save(owner: Owner) {
        do {
            try self.realm.write { 
				// 3.
                realm.add(owner  }
        } catch { */ error handing */ }

    func populate(owner: Owner) {
        let newDog = Dog()    = dogNamed
        newDog.breed   = dogBreed
        do {
            try self.realm.write {
				// 4.
        } catch { */ error handing */ }

I use a Set Collection in parallel, to check if an owners (Parent) exists. It works well.

newOwner - which at the same status of being unmanaged, or according to the docs here, ‘not residing in a realm’ - gets passed into …

… a separate concern for saving. It works. No problem here.

the Dog ( child ) details get populated in …

… because I’m passing parameters out of 2a&b scope. The function populate() doesn’t know about its managed or unmanaged status. and it does its job correctly.

Can anyone spot what is dogging me? I’m going barking mad, honest.


thisCategory is not managed by realm, so when you are adding a new object it wont get inserted.

This is not the case with the newCategory, because you added that before :slight_smile:

Note: after the 6th edit, this answer is no longer valid.


Time to admit defeat.
I edited the question heavily so that it is in same sense of the examples given on this site. This is as a public service to other newcomers who may have the same puzzle.

I appreciate that you made me think, and not just given the answer for parrot-fashion use. I learned quite a lot, except what I needed to know. humbleGrasshopper has not a blade of grass left to chew on, can prayingMantisMastah could point him in the right direction.