Add an obsrver to an NSArrayController

128 Views Asked by At

I want to make an addObserver to my NSArraycontroller. The idea is that if an record from Core Data ( bind to the NSArrayController ) is changes the record get's saved to the sqlite database. The NSArraycontroller is bind to an NSTableView.

2

There are 2 best solutions below

3
Hal Mueller On

If you register for the NSManagedObjectContextObjectsDidChange notification on your array controller's managed object context, you'll get notifications when those objects change. By using a dedicated child MOC for your view, that MOC will capture all of the changes, and you can then save your MOC to its parent.

If you want notifications based on user edits, use the tableview, not the array controller. If you're still using a cell-based tableview, consider overriding one of the (deprecated) methods like -textDidEndEditing:.

For view-based tableviews, look at the various delegates for NSControl, such as -control:textShouldEndEditing: on NSControlTextEditingDelegate. Implement them on your tableview delegate, and of course make the tableview delegate also the delegate of each control in your table (that might already happen? Check first).

How can I get notified when the user finishes editing a cell in an NSTableView? has some good discussion.

0
Erich Snijder On

Thanks a lot Hal Mueller.

By going for:

// MARK: - Textfield.
    override func controlTextDidEndEditing(_ obj: Notification) {
        print("Notificatie: \(obj)")
    }

Now i can go further.