Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid keypath passed to setPropertiesToFetch in Core Data

I get this error when using setPropertiesToFetch. It's odd because info1 and info2 do exist in my entity.

    let fetchRequest = NSFetchRequest()
    let entityDescription = NSEntityDescription.entityForName("Table1", inManagedObjectContext: managedObjectContext)
    fetchRequest.resultType = .DictionaryResultType
    fetchRequest.returnsDistinctResults = true

    fetchRequest.propertiesToFetch = ["info1", "info2"]
    fetchRequest.includesSubentities = true
    fetchRequest.entity = entityDescription

    do {
        let result = try managedObjectContext.executeFetchRequest(fetchRequest)

        print(result)

    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }

Without propertiesToFetch line I get this output:

[{
    info1 = data1;
    info2 = data2;
    info3 = data3;
}, { 
...
}]
like image 335
HelloimDarius Avatar asked Mar 04 '26 20:03

HelloimDarius


1 Answers

Entity needs to be set before propertiesToFetch can be used.

fetchRequest.entity = entityDescription
fetchRequest.propertiesToFetch = ["info1", "info2"]
fetchRequest.includesSubentities = true
like image 128
Deepak Carpenter Avatar answered Mar 07 '26 10:03

Deepak Carpenter