开发者

Calculate row counts for certain records in NSArrayController with filterPredicate applied

I have a simple app that has an NST开发者_运维问答ableView bound to a core data-backed NSArrayController.

The application displays all of the records from the core data store in the table view.

I have some user settable filters defined which set the filter predicate on the NSArrayController. These do things like filter OUT records that have a flag set

I would like to have a dashboard that lists metadata about the records in the core data store, for example:

total number of records

number of records where field active = Y

number of records where field active = N

number of records where field updated = Y

I would like this information calculated on the contents of the store, not the visible content of the NSTableView.

The issue I have is that the arrangedObjects value of the NSArrayController is affected by the application of the filters, so things like total-number-of-records changes when the filters are turned on.

I've Googled a lot and looked through my various coding books but I can't work out how to calculate these values in a way that allows me to bind them to the value of an NSTextField in IB.

Any help or suggestions would be very much appreciated.

Regards

Darren.


Why not create another controller object that calculates these values directly from the Core Data stack and exposes them in a KVO-compliant manner?


First subclass your controller, in this case NSArrayController. Second, create properties on the controller that represent the values you wish e.g:

@property (nonAtomic,retain) NSNumber *fieldActiveYes; @property (nonAtomic,retain) NSNumber *fieldActiveNo; @property (nonAtomic,retain) NSNumber *fieldChanged;

Next, create a custom getter for each property:

-(NSNumber *) fieldActiveYes{
    // ... perform a fetch by value on the field you wish
   //... set the fetch to return dictionaries only
   // ... use @sum collection operator to sum the fields
   return sum;
}

Bind the UI field's value to the controllers fieldActiveYes key. When the managedObjectContext changes, it should recalculate. If not, you might have to add some observing.


Thanks for the replies.

In the end I found this to be the simplest solution:

I created additional NSArrayControllers in IB (one for each textfield), and bound the managedObjectContext of each one to the main moc.

I set the ACs to be in entity mode, then set the FetchPredicate of each to the relevant conditional.

In my UI I then bound the each textfield to the arrangedObjects @count of the relevant new AC.

Regards

Darren.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜