Search code examples
core-datacocoa-bindingsnsoutlineview

Get sum of values from core data attributes displayed in an outline view populated from a tree controller


I am attempting to learn more about creating apps for OS X and have run into an issue that google has not been able to provide an answer for. The example app I am creating is an RSS reader. I have Core Data Entities of Folder and Feed, with Feed inheriting from Folder.

One attribute of Folder is unreadCount, with type Integer64. Feed inherits this attribute from Folder. The idea is that a display of the count of unread articles in a feed will be displayed and the total number of unread articles of all feeds contained in a folder will be displayed with that folder.

I have an NSTreeController that is managing the Entity Folder. This tree controller populates an outline view with the titles of the folders/feeds and the unreadCount for each feed and folder (right now folder is just defaulting to 0). The unreadCount is getting displayed via a value transformer that takes the integer count and returns its string version. All of this has bee set up with bindings and is working fine.

The issue I am having is in getting the unreadCount of a folder to be the sum of all the unread counts in its feeds. I have Googled for several hours and come across what appears to be a solution for tables populated from array controllers with the @sum.value, but this obviously does not work with tree controllers (from what I can determine anyway).

So, my question, how do I get the displayed unreadCount for a Folder to be the sum of the unreadCounts of the Feeds the Folder contains when the display is an outline view populated from a tree controller?


Solution

  • I suggest you rethink your data model. Instead of having Feed inherit from Folder, use two separate entities connected by a relationship: one Folder has many Feed instances.

    Your count can then be supplied by the Model layer: @sum.self.feeds. This gives you the ability to write automated tests, too, and will simplify your life when you need something like the number of unread articles in a particular folder.