0

For a NSFetchedResultsController, I am trying to create a request that satisfies any 1 of 3 possible double-conditions (1 AND "Masculine") OR (2 AND "Masculine") OR (3 AND "Masculine"). 1,2,3 are stored column integers, and "Masculine" the value of a to-one relationship from a CellData entity to Gender.text (.text is indexed).

enter image description here

The data seems correct in CoreData. There is a column value, and PK values for Gender rows are assigned to the coreData.gender relationship attribute (see below).

BUT - I am getting an error: keypath cellData.gender.text not found in entity

I suspect it is the way I am creating my Predicates or Compound Predicates as this is new to me.

Initially, cellData.gender.text predicates are defined with a value matching an existing value in DB:

var col0GenderPredicate: NSPredicate = NSPredicate(format: "cellData.gender.text == %@", "Masculine")
var col2GenderPredicate: NSPredicate = NSPredicate(format: "cellData.gender.text == %@", "Masculine")
var col3GenderPredicate: NSPredicate = NSPredicate(format: "cellData.gender.text == %@", "Masculine")

In the fetched results controller, predicates are: 1) Defined for cellData.column values. 2) Compounded into corresponding pairs of column AND cellData.gender.text values. 3) Further compounded as an OR compound predicate.

@objc private func loadSavedData()
{
    if fetchedResultsController == nil
    {
        print("creating fetchresultsController")
        let request = CellData.createFetchRequest()    
        let sortDescriptor = NSSortDescriptor(key: "sortOrder", ascending: true)    // sort by name
        request.sortDescriptors = [sortDescriptor]     
        fetchedResultsController = NSFetchedResultsController(fetchRequest: request,
                                                              managedObjectContext: container.viewContext,
                                                              sectionNameKeyPath: nil,
                                                              cacheName: nil) as? NSFetchedResultsController<NSFetchRequestResult>

// 1)
        let column0Predicate = NSPredicate(format: "column == %ld", 1)
        let column2Predicate = NSPredicate(format: "column == %ld", 3)
        let column3Predicate = NSPredicate(format: "column == %ld", 4)

// 2)   
        let col0AndGenderPredicate = NSCompoundPredicate(type: .and,
                                                         subpredicates: [column0Predicate, col0GenderPredicate])
        let col2AndGenderPredicate = NSCompoundPredicate(type: .and,
                                                                 subpredicates: [column2Predicate, col2GenderPredicate])
        let col3AndGenderPredicate = NSCompoundPredicate(type: .and,
                                                         subpredicates: [column3Predicate, col3GenderPredicate])

// 3)
        let columnAndGenderPredicate = NSCompoundPredicate(type: .or, subpredicates: [col0AndGenderPredicate, col2AndGenderPredicate, col3AndGenderPredicate])

        fetchedResultsController?.fetchRequest.predicate = columnAndGenderPredicate
        fetchedResultsController?.delegate = self
    }

    do { try fetchedResultsController?.performFetch() }
    catch { print(error.localizedDescription) }
}

Then I get an error: error: SQLCore dispatchRequest: exception handling request: , keypath cellData.gender.text not found in entity with userInfo of (null)

But cellData.gender is in CoreData: enter image description here

And gender.text is also there: enter image description here

Other discussions suggest an error with FetchedResultsController, but it was working until I started chaining the Predicates, and I can't find anything highlighting the problem there or anything to try.

DrWhat
  • 2,360
  • 5
  • 19
  • 33
  • Tip: [Xcode 4 and Core Data: How to enable SQL Debugging](https://stackoverflow.com/questions/6428630/xcode-4-and-core-data-how-to-enable-sql-debugging) – Willeke Jul 24 '19 at 10:36

1 Answers1

0

You are creating your NSFetchedResultsController to request data from CellData so then the keypath should be gender.text only

 var col0GenderPredicate: NSPredicate = NSPredicate(format: "gender.text == %@", "Masculine")

Also, compared to what you write and the images it looks like you have a typo here

 let column0Predicate = NSPredicate(format: "column == %ld", 0)

shouldn't it be 1 rather than 0 or is the text in the question wrong?

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • gosh! I don't know why I thought it would be different. Relationships scare me. I corrected the column typo in the question - thx. On to the next breakpoint! – DrWhat Jul 24 '19 at 11:08