5

I'm trying to fetch all the children of a parent.

In my case the parent is an entity AgendaEvent which has many AgendaDate (the children).

so Here's my function:

 func getRelatedAgendaEvent(event: AgendaEvent) ->NSFetchRequest<AgendaDate> {
    // create a fetch request that will retrieve all the AgendaDate.
    let fetchRquest = NSFetchRequest<AgendaDate>(entityName: "AgendaDate")

    // set the predicate to only keep AgendaDate related with the AgendaEvent selected
    fetchRquest.predicate = NSPredicate(format: "parent == %@", event)

    return fetchRquest
}

and I use this in didselectRow for a tableView:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var eventToPass: AgendaEvent!
    var eventDateToPassArray: [AgendaDate]!

    eventToPass = myTempEvents[indexPath.row]
    eventDateToPassArray = try! context.fetch(getRelatedAgendaEvent(event: eventToPass))
    DispatchQueue.main.async { () -> Void in
        self.performSegue(withIdentifier: "EventToModify", sender: eventToPass)
    }
}

}

I'm trying to use eventDateToPassArray in a segue. The error i'm getting is:

keypath parent not found in entity <NSSQLEntity AgendaDate id=1> with userInfo of (null)

I'm not sure this is the right path. I am trying to update a NSSet (AgendaDate) when the user edit an AgendaEvent. So basically while updating and AgendaEvent the user also updates the date in the related AgendaData NSSet.

Thank you!

-------------UPDDATE

Martin you mean this:

AgendaDate+CoreDataProperties.swift

extension AgendaDate {

@nonobjc public class func fetchRequest() -> NSFetchRequest<AgendaDate> {
    return NSFetchRequest<AgendaDate>(entityName: "AgendaDate")
}

@NSManaged public var agendaDates: NSDate?
@NSManaged public var agendaEvents: AgendaEvent?

}

AgendaDate+CoreDataClass.swift

import Foundation
import CoreData

@objc(AgendaDate)
public class AgendaDate: NSManagedObject {

}
Marco
  • 1,051
  • 1
  • 17
  • 40
  • You assign a value to eventDateToPassArray, but never *use* it. – Martin R Sep 23 '17 at 14:06
  • @MartinR i know.... i've printed just to see the results..but i get that error – Marco Sep 23 '17 at 14:08
  • Show how `AgendaDate` is defined. And where *exactly* do you get the error? – Martin R Sep 23 '17 at 14:08
  • @MartinR i get the error when i select the AgendaEvent in the table. How is defined you mean the object or in the controller? – Marco Sep 23 '17 at 14:10
  • Show the class/extension code where the properties of AgendaDate are defined. – Martin R Sep 23 '17 at 14:12
  • @MartinR sorry... i'm new to this... i've created it in interface builder and it does not automatically create any extension or class...i can add a picture of the model in interface builder – Marco Sep 23 '17 at 14:15
  • @MartinR I have updated question – Marco Sep 23 '17 at 14:24
  • It seems that you nowhere defined a "parent" property in AgendaDate (which explains the error message). – Martin R Sep 23 '17 at 14:29
  • but i defined the relationship... i guess i have so much to learn still... i thought the parent was the relationship between the 2 entities... – Marco Sep 23 '17 at 14:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155153/discussion-between-martin-r-and-marco). – Martin R Sep 23 '17 at 14:33

1 Answers1

3

The key path used in the fetch request must match the actual name of the Core Data relationship, in your case "agendaEvents", there is no implicit "parent" relationship:

fetchRquest.predicate = NSPredicate(format: "agendaEvents == %@", event)

Even better, use #keyPath and the %K expansion

fetchRquest.predicate = NSPredicate(format: "%K == %@",
                                    #keyPath(AgendaDate.agendaEvents), event)

so the that compiler checks the validity of the key path. This helps to avoid using unknown key paths or typos.

Note that a better relationship name would be "agendaEvent" or just "event", with the singular form for a to-one relationship.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382