How to retrieve all records that have a common parent relation

I have set a core data structure based on two entities :

  • first entity is "Mot" (Word) with one String attribute "graphie" (spelling) and one ToOne relationship "definition" with the second entity.

  • second entity is "Definition" that has one String attribute "enonce" (statement) and on ToMany relationship "mot" with entity Mot.

One word can have several spellings but only one definition.

I managed to load several rows of data and now, when I select one word I want to display all the spellings, which means all "Mot" that have the same definition.

In my content view I have a List based on a request:

@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(entity: Mot.entity(), sortDescriptors: [NSSortDescriptor(key: "graphie", ascending: true)])
private var mots: FetchedResults<Mot>

var body: some View {
    NavigationView { 

...

                    List {
                        ForEach(mots) { (mot: Mot) in
                            HStack {
                                Text(mot.graphie ?? "Non trouvé")
                                Spacer()
                                Text(mot.definition!.enonce ?? "Non trouvé")
                            }
                            .onTapGesture {
                                motAffiche = mot
                                tfMot = mot.graphie ?? "Not found"
                                tfDefinition = mot.definition?.enonce ?? "Not found"
                                tfOrthoAlt = returnOrthoAlt(mot: mot)
                            }
                        }
                    }

the function returnOrthoAlt(mot) is supposed to return a string with all spellings separated by commas.

What is working for the moment, but not satisfactory is the following code for that function

private func returnOrthoAlt(mot: Mot) -> String {
    
    var ort = [String]()
    
    for elem in mots {
        if elem.definition!.enonce! == mot.definition!.enonce! && elem != mot {
            ort.append(elem.graphie!)
            let defObjId = elem.definition!.objectID.uriRepresentation()
            print("\(elem.graphie!) def: \(elem.definition!.enonce!) \(defObjId) ")
        }
    }
    
    return if !ort.isEmpty { ort.joined(separator: ", ")} else {""}
}:

I am going through the table 'mots' of FetchedResults to find those that have the same definition as the current 'mot'. What I find not satisfactory is that I am comparing the 'mot.definition.enonce' of each Mot, which is not supposed to be unique, instead of 'mot.definition' directly, which did not work. The objects 'mot.definition' are obviously not equal (neither with == nor with === operators).

Also I tried to retrieve the reverse relation directly with 'mot.definition.mot' but that returned a NSSet which, converted to a set<Mot> seems to contain only one object, that is 'mot' itself.

One possibility of course, would be to ad a unique Id to the entity 'Definition' but I seem to understand that this is not the recommended practice, the more so as swift does not provide system generated id.

What do I miss in the core data concept ? Can someone help me out ?

Replies

Ok I found the mistake in my code. Had nothing to do with Core Data, sorry. Anyone who has the authorization to do so, please delete the post. Thanks !