Core Data: Fatal Error sorting using a computed property

I have a Core Data entity with a computed property and I want to sort the objects of this entity by this computed property. So

• I build the NSFetchRequest in the normal way, including an NSSortDescriptor for sorting by this computed property.

• Construct the usual NSFetchedResultController and call performFetch().

This results in a fatal error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath score not found in entity Wine'

I've tried to do this in several different ways, including (a)

@objc public var score: Float {
    get {
        <getter code here>
    }
}

(b) Define score in the data model as transient, then

@objc public var _score: Float {
    get {
        <getter code here>
    }
}
    ... and ...

public override func awakeFromFetch() {
    super.awakeFromFetch()
    score = _score
}

But the error persists.

Is there any way to get this to work without abandoning NSFetchedResultsController, fetching unsorted records, sorting them programmatically and building the snapshot from the sorted array of objects? (I know, but it's the best I've been able to come up with so far...)

Replies

I have this same problem.

  • How did you end up solving it?

Add a Comment

I finally worked out a solution to this problem, but it is a real hack. If you're interested in details, I can write it all up.

However, in a nutshell, what I do is as follows:

  • in any entity which contains an attribute that is used in the computation for the computed property, I use the validateForXXX methods to register changes. (The registration process is part of my CoreDataStack.)
  • The entity which has the "computed" properties maintains real attributes with the computed values. The entity registers for notification for changes and performs the computations necessary to update the real (stored) values as appropriate.

In this way, I am able to use a stored attribute (which can be the target of the find or sort operation) which is kept up to date. It's definitely kludgy (and requires more infrastructure that needs to be implemented correctly and consistently that I'd like), but I am finding it to be reliable.

Again, I'll prepare a writeup if you're interested.

After spending three hours on this yesterday, I would absolutely be interested in a write-up or some pseudocode 😀

I commented in another thread about this in terms of hoping others can shortcut their search!

I put together a writeup of the technique I'm using. (Yes, it's kludgy and I would be delighted for someone to offer an improvement or a replacement.)

If the writeup seems long (or insultingly detailed), please forgive me. I find writing this sort of thing helps me clarify my thinking.

Be that as it may, since I can't attach either a Pages file or a PDF to this reply, I moved the writeup to my Dropbox. https://www.dropbox.com/s/nf2diu6haayn147/Pseudo-Transient%20Attributes.pdf?dl=0

  • Thank you for the writeup!

Add a Comment