Coredata and swift: relation between objects disappears

I have a relation between 2 objects that works fine. I create it and then I try to fetch one object from the other object (with the relation) and it works. But after a certain time this relation stops working and trying to get one object from the other gives mean error (with exactly the same code). I'm trying to see where this happens exactly but there isn't a particular part of the code where this relation is forgotten. Sometimes is in one line and sometimes in another different line. The relations are shown in the image. The entities that I'm using are TopicData and WordData, so just a one-to-many relation.

I have no constraint attributes and codegene for all entities is Manual/None. After finishing the core data file, I have created NSManagedObject subclasses for all the entities. And in these subclasses I have added some functions to fetch objects. My question is why could it happen that relations disappear. I wonder if it could be related to the core data codegen and creating my own functions inside those subclasses. As an example, here is the code of the generated subclass GroupData+CoreDataProperties, where I added my own functions. The function getTopicFromGroup is the one that works fine first but then crashes (there isn't a topic for the group)


import Foundation

import CoreData

extension GroupData {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<GroupData> {

        return NSFetchRequest<GroupData>(entityName: "GroupData")

    }

    

    static func getGroupFromIndexAndTopic(index: Int16, topic: TopicData, context:NSManagedObjectContext) -> GroupData {

        let fetchRequest = NSFetchRequest<GroupData>(entityName: "GroupData")

        

        fetchRequest.predicate = NSPredicate(format: "index_group = %d AND topic = %@", index, topic)



        let groups = (try? context.fetch(fetchRequest)) ?? []

        let group = groups.first!

        return group

    }
 @NSManaged public var cleared_words: Int16

    @NSManaged public var index_group: Int16

    @NSManaged public var mean_level: Int16

    @NSManaged public var topic: TopicData?

    @NSManaged public var word: NSSet?



}



// MARK: Generated accessors for word

extension GroupData {



    @objc(addWordObject:)

    @NSManaged public func addToWord(_ value: Word)



    @objc(removeWordObject:)

    @NSManaged public func removeFromWord(_ value: Word)



    @objc(addWord:)

    @NSManaged public func addToWord(_ values: NSSet)



    @objc(removeWord:)

    @NSManaged public func removeFromWord(_ values: NSSet)



}



extension GroupData : Identifiable {



}

And the function GroupData+CoreDataClass is like it was created, I didn't add anything


import Foundation

import CoreData



@objc(GroupData)

public class GroupData: NSManagedObject {



}

I recommend turning on full Core Data debugging, see if it highlights any issues.

https://useyourloaf.com/blog/debugging-core-data/

Coredata and swift: relation between objects disappears
 
 
Q