0

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 Answers2

1

Thanks a lot Hal Mueller.

By going for:

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

Now i can go further.

0

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.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • You get notifications when any objects change. – Willeke Jun 20 '17 at 21:18
  • I try'd the tutorial add the following site but doesn't do what iwant. https://cocoacasts.com/how-to-observe-a-managed-object-context/ The notification only takes place when de save command is executed. What is it that goes wrong. I need an addObserver that takes place when an textfield in the NSTableView is edited. – Erich Snijder Jun 23 '17 at 18:02
  • Thanks a lot Hal Mueller. Bij going for: // MARK: - Textfield. override func controlTextDidEndEditing(_ obj: Notification) { print("Notificatie: \(obj)") } – Erich Snijder Jun 27 '17 at 17:00