0

I have a CoreData entity with a date attribute and a computed property called 'month'. The month is computed with this method in the entity class:

var month: String {
    get {

        let dayTimePeriodFormatter = NSDateFormatter()
        dayTimePeriodFormatter.dateFormat = "YYYY, MMMM"

        let dateString = dayTimePeriodFormatter.stringFromDate(self.date!)

        return dateString

    }
}

The fetched result controller is set up with this method:

func loadDataSourceForProductionMonthHistory(month: String){

    let suppliersFetchRequest = NSFetchRequest(entityName: "Production_Day")
    let sortDesciptor = NSSortDescriptor(key: "date", ascending: true)
    suppliersFetchRequest.predicate = NSPredicate(format: "month = %@", month)
    suppliersFetchRequest.sortDescriptors = [sortDesciptor]

    fetchedResultsController = NSFetchedResultsController(fetchRequest: suppliersFetchRequest, managedObjectContext: self.context, sectionNameKeyPath: nil, cacheName: nil)

    fetchedResultsController.delegate = self

    do {
        try fetchedResultsController.performFetch()
    } catch {
        print("Error occured with FRC")
    }
}

And the error thrown is:

2016-06-10 15:25:55.788 Stock Controller[11404:834892] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath month not found in entity <NSSQLEntity Production_Day id=11>'

Is it possible to filter with a predicate on a computed property? I had been using the computed property to create section headers for grouping so thought it would work for this?

I could just create a month attribute but hoping I can avoid and extra attribute that has to be maintain just for filtering.

SimonBarker
  • 1,384
  • 2
  • 16
  • 31
  • 2
    It has been asnwered here: http://stackoverflow.com/questions/13325849/sorting-on-transient-fields-with-nsfetchedresultcontroller – srvv Jun 10 '16 at 15:07
  • Damn, thanks. My understanding was that transient and computed weren't quite the same so might still be possible. Any reason why this is the case? Something like: Computed attrs don't exist until they are fetched and the predicated attr is needed before the fetch? – SimonBarker Jun 10 '16 at 15:59
  • Since it's part of fetch request, I guess it will be finally translated into SQL/database access query. But that is not possible since its not an attribute in the schema. – srvv Jun 10 '16 at 17:40
  • But why not filter/sort after fetching from database? I guess since properties fault only when accessed, the computed property will still be unavailable. – srvv Jun 10 '16 at 17:51
  • If I filter after fetching I think I loose the benefits of the fetchedResultController - I'll just ad a couple of extra fields for month and year – SimonBarker Jun 11 '16 at 09:39

0 Answers0