How to use NSOutlineView with multiple core data entities as groups

279 Views Asked by At

I have been looking into how to populate a Source List from core data using multiple entities. If I had one entity, I can follow documentation and use a NSTreeController to populate and bind my data to the NSOutlineView.

In my specific situation I have around 3 hard-coded groups, where each one of my groups would relate to a separate core data entity. I attempted to combine them but they all have relationships to different entities so I wouldn't be able to combine them all in one core data entity. I ideally wanted to bind my 3 core data entities to my Source List where they look something like the following (Where Pens, Papers and Desks are all separate entities) :

enter image description here

Does anyone know of anyway I'd be able to use a controller to help mange and combine three entities with my Source List or would the best approach be to manually populate NSOutlineView? Can I use some sort of array of NSArrayControllers to populate my NSOutlineView? Is there a way I can override NSTreeController to take in data from three sources?

1

There are 1 best solutions below

0
Willeke On

As an experiment I created a project with an outline view and tree controller. The contents of the tree controller is an array of dictionary, the array controllers (in entity mode) are outlets:

treeController.content = [
    ["arrayController": penTreeArrayController, "name": "Pens"],
    ["arrayController": paperTreeArrayController, "name": "Papers"],
    ["arrayController": deskTreeArrayController, "name": "Desks"]
]

The tree controller is a subclass of NSTreeController:

override func childrenKeyPath(for node: NSTreeNode) -> String? {
    if node.representedObject is NSManagedObject {
        return nil
    }
    return "arrayController.arrangedObjects"
}

It seems to work. When I add or remove a pen, the outline view automagically updates. It doesn't work the other way around, I can't use the tree controller to add a pen.