Search code examples
objective-ccocoacore-datacocoa-bindings

Core Data: Sorting results from an abstract entity based on entity type


Is it possible to sort results from Core Data based on the entity type, but:

  • without resorting to adding a fake attribute called something like entityType, and
  • without adversely affecting performance by using KVO/KVC tricks such as introducing -(NSString*)typeOfEntity or similar?

I currently have:

  • TradeDocument as an abstract entity, with
  • QuoteTradeDocument and InvoiceTradeDocument as entities based on it.

I want to display entity type and/or allow NSTableView to be sorted based on this.

I use Cocoa Bindings on OS X.


Note: I am explicitly trying to avoid faulting each object.


Solution

  • Ivan,

    The only limit with an abstract entity is that you can't instantiate one. You can still fetch them. Then it is a rather simple matter of testing against which subclass each managed object is.

    Sorting on type is not something a sort descriptor can do but a function can, as with -sortUsingFunction:context:.

    Andrew