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).
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:
And gender.text is also there:
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.