3
votes

i have some Problems with my macOS Application and hope you can help me. Im pretty new on macOS so please be nice ;)

A part of the app consists of a simple NSTableView which content is binded to an NSArrayController. The NSArrayController is feeded from a realm database.

TableView

As you can see there is a checkbox for each row, which should set the bool value in the realm object.

The bindings are ok, so if I mark/unmark the checkbox it seems to try writing on the realm object. But since realm needs a active write transaction, which will not be triggered, it crashes.

My Question is: How can I write on realm objects with table view bindings ? Any ideas ?

1
Can you tell me how did you used nsarraycontroller with realm ?Johnykutty
Im in the road now, but tonight I'll give you a snippet ;)Peter C. Glade
Thanks man :). Have used array controller with coredata. But in some cases(a table contains 300k records) filtering is too slow.Johnykutty
CoreData is horrible 🙈Peter C. Glade
Sorry for my late response. I don't find the code or this by now, but it wasn't difficult at all. The whole thing is to bind the array controller to you tableView like you normally do, and alter the array controller content via the realm notification block.Peter C. Glade

1 Answers

1
votes

You could try to add the following to your model object (the Realm object) :

override func setValue(_ value: Any?, forKey key: String) {
    try! realm?.write {
        self.mySetValue(value, forKeyPath: key) // bug in swift preventing directly calling super here
    }
}

private func mySetValue (_ value: Any?, forKeyPath key: String) {
    super.setValue(value, forKey: key)
}