Searched entire Internet but couldn’t find the modern solution for my problem. I want to use NSObjectController in pair with Core Data through Cocoa Bindings and struggle to set it up properly. Worth noting that I’m using latest version of Xcode and Swift.
What I’ve done:
For testing purposes I’ve done the following:
- Created an macOS app with “Use Core Data” option selected (the app is not document based);
- Dragged 2 NSTextFields into the Storyboard Dragged NSObjectController to the view controller scene;
- Added Employee Entity to Core Data model with 2 attributes “name” and “surname”;
- Done everything from the answer in How do I bind my Array Controller to my core data model?
- Set NSObjectController to entity mode and typed in “Employee”,
- Prepares Content selected, Use Lazy Fetching selected so all three options checked;
- Binded the NSObjectController’s Managed Object Context in bindings inspector to the View Controller’s managedObjectContext;
- Binded NSTextFields as follows: Value - Object Controller, Controller key - selection, Model Key Path - name (for 1st text field) and surname (for 2nd).
That’s it.
First set of questions: What I did wrong and how to fix it if it’s not completely wrong approach?
I’ve read in some post on stackoverflow that doing it that way allows automatic saving and fetching from Core Data model. That’s why I assumed it should work.
So here is a Second set of questions: Is it true? If it is then why text fields are not filled when view is displayed? If it is not then how to achieve it if possible (trying to write as less code as possible)?
Third question: If I used approach that is completely wrong would someone help me to connect Core Data and NSObjectController using Cocoa bindings and show me the way of doing so with as less code written as possible using the right approach?
Taking into account that there no fresh posts about this topic in the wilds I think the right answer could help a lot of people that are developing a macOS app.
Thanks in advance!