Issue when using Ream Swift

Hi,

I am using the Realm-Swift SDK to create some models but I am having an issue and I can’t work it out.

I have a class that is a top level “group”:

import RealmSwift

class InventoryGroup: Object, Identifiable, Encodable {
    @objc dynamic var id: UUID = UUID()
    @objc dynamic var name: String = ""
    @Published dynamic var sections: RealmSwift.List<InventorySection> = RealmSwift.List<InventorySection>()

    enum CodingKeys: String, CodingKey {
      case name,
           id,
           sections
    }

    func encode(to encoder: Encoder) throws {
      var container = encoder.container(keyedBy: CodingKeys.self)
        
      try container.encode(name, forKey: .name)
      try container.encode(id, forKey: .id)
      try container.encode(sections, forKey: .sections)
    }

    override init() {
        super.init()
    }

    init(_ name: String) {
        super.init()

        self.id = UUID()
        self.sections = RealmSwift.List<InventorySection>()
        self.name = name
        return
    }

    func addSection(_ name: String) {
        sections.append(InventorySection(name))
    }
}

This then has sections (or a sub group):

import Foundation
import RealmSwift

class InventorySection: Object, Identifiable, Encodable {
    @objc dynamic var id: UUID = UUID()
    var items: RealmSwift.List<InventoryItem> = RealmSwift.List<InventoryItem>()
    @objc dynamic var name: String = ""

    enum CodingKeys: String, CodingKey {
      case name,
           id,
           items
    }

    override init() {
        super.init()
    }
    
    init(_ name: String) {
        super.init()
        self.id = UUID()
        self.items = RealmSwift.List<InventoryItem>()
        self.name = name
        return
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encode(name, forKey: .name)
        try container.encode(id, forKey: .id)
        try container.encode(items, forKey: .items)
    }

    func setupTest1() {
        self.items.append(InventoryItem("Sample Item 1"))
    }

    func setupTest2() {
        self.items.append(InventoryItem("Sample Item 2"))
    }
}

and then each section holds multiple items:

import SwiftUI
import RealmSwift

class InventoryItem: Object, Identifiable, Encodable {
    @objc dynamic var id: UUID = UUID()
    @objc dynamic var name: String = ""
    @objc dynamic var notes: String = ""
    @Published dynamic var state: ItemState = .unchecked

    enum CodingKeys: String, CodingKey {
      case name,
           id,
           state,
           notes
    }

    func setItemState(_ newState: ItemState) {
        self.state = newState
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encode(name, forKey: .name)
        try container.encode(id, forKey: .id)
        try container.encode(state, forKey: .state)
        try container.encode(notes, forKey: .notes)
    }

    override init() {
        super.init()
    }

    init(_ name: String) {
        super.init()
        self.id = UUID()
        self.name = name
        self.state = .unchecked
        self.notes = ""
    }

    enum ItemState: String, Encodable {
        case pass
        case fail
        case unchecked 
    }
}

in my ContentView I have a variable:

var inventoryGroup: InventoryGroup = .init("KV57P1")

                      let sec1 = InventorySection("Right Front")
                        sec1.setupTest1()
                        self.inventoryGroup.sections.append(sec1)

however this always produces error:

Terminating app due to uncaught exception ‘RLMException’, reason: ‘Only objects which are managed by a Realm support change notifications’

What am I missing with this, or what have I setup incorrectly.

Any help would be appreciated