Using NSTableView with cocoa binding and Realm


#1

I am using NSTableView with cocoa binding.
The Array which is binding with my table is causing an issue when I tried to save it to Realm.

Error : Error Cannot add an object with observers to a Realm

Is there a workaround to use Realm with NSTableview cocoa binding variable ?

class Dog: Object {
    @objc dynamic var owner: Person?
    @objc dynamic var name = ""
    var age = RealmOptional<Int>()
    let owners = LinkingObjects(fromType: Person.self, property: "dogs") // inverse
}

class Person: Object {
    @objc dynamic var name = ""
    @objc dynamic var birthdate = Date(timeIntervalSince1970: 1)
    let dogs = List<Dog>() // to-many
}
let realm = try! Realm()
@objc dynamic var dogsArray = [Dog]() // Cocoa binding with my NSTableView


override func viewDidLoad() {
    super.viewDidLoad()

    let dog1 = Dog(value: ["Fido", 5])
    let dog2 = Dog(value: ["Pluto"])
    dogsArray = [dog1,dog2]


    let person = Person(value: ["Alex", Date()])
    person.dogs.append(objectsIn: dogsArray)

    try! realm.write { realm.add(person) }
}