NSOutlineView with Bindings driven by Core Data - cocoa

I've got an NSOutlineView acting as a source list for my application, and my data model is done with Core Data. I'd like to use bindings (if possible) to glue these things together as follows:
I have three main entities in my model, for sake of example let's call them "Stores", "Cars" and "People".
My goal is to have the outline view have 3 "groups" (expandable nodes, like PLAYLISTS in iTunes), each group representing and listing one of my entities, and also I've got three buttons at the bottom of my window to "Add Store", "Add Car", etc which I'd like to have wired up to perform that action.
So far in my window's nib I've got a TreeController which is bound to my NSManagedObjectContext instance of my window controller, but I can't figure out how to properly bind and populate the outline view from the TreeController.
Is this possible with bindings? I've seen one tutorial where a second managed object model is created, with entities for the outline nodes, but some comments on the article said this was a bad idea. I'm not really sure how to proceed, any help would be wonderful!

Try the Cocoa Bindings Programming topics: Providing Controller Content section
What I did was create custom classes for my entities, and added isLeaf properties to them. For the top level (Stores, Cars, People) I return no. For leaf nodes (a car, a person, etc) I return YES.
The top level needs to have a to-many relationship to the leaf nodes, I called this children.
In Interface Builder, I set the NSTreeController's mode to Entity, name: Groups. It's bound to the managedObjectContext. In the Key Paths I set the Children attribute to children, and Leaf to isLeaf.

Related

Can I add an NSManagedObject to the selected parent object just using Interface Builder and Core Data?

I'm learning Core Data and at the moment just using Interface Builder and the XCode data model (following the Aaron Hillegass Cocoa book examples).
I have an MVC application and core data set up and working within interface builder (no hand-written code yet). I've set up various arraycontrollers, views and buttons to add and remove objects and edit data and determine relationships between objects.
However my object model is strictly hierarchical parent-child (and grandchildren) and each relationship is one-to-many. So when I add a child (or grandchild) I want to automatically assign the parent object currently selected in the main tableView as the parent of the new child object.
Is this possible in interface builder or is this the point where I have to start coding my own methods?
Any pointers on the next step I should be looking to take or how to figure it out myself much appreciated. I'm just a bit stuck and can't find a relevant tutorial or reference.
Perhaps it will help if I describe my document window. It has two tableViews with add/remove buttons. The tableView on the right displays an NSSet of the child objects of the parent objected selected in the tableView on left. I want the add button on the right to add a new object and automatically make it a child of the selected parent object so it appears in the tableView on the right.
At the moment the only methods I can see on the arrayController for the the child objects are add: and remove: but of course they don't connect the new object to the currently selected parent. Do I need to add my own (IBAction) method to the parent object i.e. addChildObject:? Or is there a built in way to add the child automatically within CoreData based on the parent-child relationship?
Problem solved. I found a tutorial (albeit a bit dated) describing exactly what I am trying to do:
https://developer.apple.com/cocoa/coredatatutorial/index.html
And after studiously following every step carefully I realised I had done everything right I had just missed one binding: I had only set the Content Set of the child array controller and not its Managed Object Context (to File's Owner). That is the only reason my add button was not adding a new child object! Trivial mistake, but when you are not confident you question you're whole approach rather than looking for a simple error. Sorry for anyone who had to read this but maybe the link to the tutorial will help someone.

Is it possible to bind an NSTreeController to an NSOutlineViewDataSource?

I have some hierarchical data model that I'd like to present in an NSOutlineView. I'm binding a tree controller to the outline view to provide data and to handle selection and binding to other views.
However, I only want to show only part of the data in my model to the outline view. (Each object in my hierarchy has an array of child objects, but I'd only like some of these child objects to appear as child nodes of the node in the tree.) I wish I could just attach a filter predicate to the tree controller, but it seems that NSOutlineView doesn't support filter predicates.
I think that this design requires an NSOutlineViewDataSource to filter my data model, and an NSTreeController bound to the data source and the outline view. However, none of the binding outlets in the tree controller (Content Array, Content Object, Content Set, etc.) seems appropriate for binding a data source.
Any ideas? Thanks in advance...
You can try feeding the data to your array of child objects through an array controller.
Here's how I'd do it. Override the accessor method in your represented object and return a filtered array from your array controller.
In general, NS[Outline|Table]ViewDataSource and Cocoa Bindings is an "either/or" choice. Mixing the approaches, while perhaps not strictly impossible, will likely lead to unpredictable results.
You mention binding a filter predicate to the Outline View itself and not to specific nodes, so I surmise that one filter predicate for all nodes might be "good enough." If that's the case, then one solution to this would be to expose a second children-vending property on your model, maybe filteredChildren, and tell the NSOutlineView to use that to access children instead of the your default/complete children-vending property. If you need functionality like drag reordering, this approach might prove to be non-trivial, but it should be easy to explore this approach regardless.
If you need a different filter for each node, or if the filter changes dynamically, this task would likely have crossed over into being a case that was a good candidate for implementing NSOutlineViewDataSource (and a poor candidate for using Cocoa Bindings.)

NSArrayControllers and heterogeneous arrays of Core Data objects

I'm trying to create a Mac OS Core Data application that has an array of parent objects (called Levels) each of which contains a collection of child objects (called Blocks) via a one-to-many relationship. I have a table view successfully controlling the array of levels, and a custom view object that draws the blocks graphically based on positions held in x and y properties of my Block model class. I can add blocks to the currently selected level, remove them, select and move them around in the custom view, and have bound text fields to various other properties of the Block class which I can use to edit those values. All of this information is successfully saved and restored to and from the core data repository with no issues output to the debugger. Wonderful. I've used an NSArrayController for the Levels and another for the Blocks that is bound to the current selection of the Levels array controller, in what I've read is a pretty standard way.
Now, my Block class is actually an abstract class, and what I actually instantiate are various child classes of Block (eg RedBlock, GreenBlock, BlueBlock classes). Each sub-class has a separate set of properties that only apply to that type of block (so RedBlock has a "text" property that none of the others have, BlueBlock has an integer "value" property, etc). I want to create an inspector that will change depending on the type of the Blocks that are currently selected in my custom view. To try this, before I start creating subviews for each type of Bock, I have created a text field that I want to bind to the currently selected RedBlock's "text" property, preferably showing nothing when Blocks of other kinds are selected. This is where I'm stuck. I've added another NSArrayController in Entity mode with RedBlock specified as it's type so I can bind to the "text" property, and tried adding a filter predicate based on the class type. I've also tried various other configurations and bindings, but I'm either getting exceptions, or corrupt values in the text field that I bind to that controller, or other weird bugs and general brokenness.
I've googled around for an example of an inspector that can cope with a heterogenous array of objects (as that's essentially what I'm trying to do) but so far no luck.
So, my question is - am I going about this the right way? Should I be trying to create an NSArrayController that filters the selected items in my Blocks array controller somehow? If so, should that be straightforward or is there some trick that I've missed? If not, what is the best way to do this?
This approach should work, provided you limit the inspector to displaying view which bind to properties which apply to the entire selection. You don't need a second array controller.
To test the simple example, try creating a data set with only blocks, see that your bound control loads without raising exceptions, and that it updates the object correctly.
Once that is working, create separate views for each type, and display and hide them when the selection will change. Again, if you have a heterogenous selection, hide them all.

Trying to use NSManagedObjectModel through NSTreeController and NSBrowser

I'm trying to use NSManagedObjectModel through NSTreeController and NSBrowser. I found some simple tutorials on the Net but they don't solve all my problems. Right now I can: add nodes (except in the leaf); to show children's nodes selecting the parent. My model is heterogeneous, all entities are different, but have the same properties: "name"; "children" (except the leaf); "parent (except the root). My model is all generated by the XCode. I don't have entered any code and don't have changed anything, all is generated by the XCode and Interface Builder.
I have 3 questions:
(1) How can I avoid the "children key error" when I reach the leaf entity?
(2) How can I avoid the creation of a children in the column of a parent?
(3) How can I disable the add children button if no one parent is selected?
(Thanks about some help. :D )
With regard to question 1: I have all my nodes implement all methods, but the leaf entities return nil for the children getter method, for example, and the root returns nil for parent.
Unfortunately, I haven't used NSBrowser, I use this technique for NSOutlineView and NSTreeController, so I can't help with questions 2 & 3.

Simply adding data to an NSBrowser?

I have a tree data structure which I would like to put into an NSBrowser. I have found complicated methods that involve App Delegates, but I would just like to insert the rows as I come across them.
An NSBrowser is a view. A view generally doesn't hold onto a whole complex tree of model objects—that's a controller's job. That's why you appoint your custom controller (and owner of your model objects) as the delegate of the browser: You own your entire model, and the view gets parts of it from you as it needs them.

Resources