Search code examples
swiftcore-dataone-to-manyentity-relationship

Why is core data is not retrieving relationship values through extension?


I have 2 entities in Core Data, Plans and Places. They are connected with one-to-many relationship (one event in a plan ("Space Mountain"), to many in Places "Magic Kingdom")).

The expectation is the user can tap on an item, like "Space Mountain", choose a date, and that item and selected date are added to the itinerary. The itinerary shows "Magic Kingdom" which I've also successfully added to the Places Entity.

It's all working, except retrieving from the relationship. The title and date (stored in Plans) show up fine 👍, but the associated location and park (stored in Places) return my nil string 👎.

Any idea what I'm doing wrong? I've gone back through all the videos and books and everything looks right to me. Thanks - still new.

    
       let plans: Plans
       var body: some View {
        
            HStack(alignment: .top) {
                
                VStack(alignment: .leading, spacing: 2) {
                    Text(plans.extTitle)
                        .bold()
                    Text("\(plans.extDate, style: .date)")
                        .font(.caption2)
                    Text(plans.extLocation)
                        .font(.caption2)
                    Text(plans.extPark)
                        .font(.caption2)

The values are returned from extensions (placesrel is the name of the relationship):

extension Plans {
    
    var extTitle: String {
        return title ?? "Undefined"
    }
    var extDate: Date {
        return date ?? Date()
    }
    var extLocation: String {
        return placesrel?.location ?? "Error Retrieving Through Extension"
    }
    var extPark: String {
        return placesrel?.park ?? "Error Retrieving Through Extension"
    }

The data is all being pulled from a struct, if that matters (no typing in from the user, just tapping)

var rides = [
    Ride(index: 1, name: "Space Mountain", location: "Tomorrowland", park: "Magic Kingdom"),
    Ride(index: 2, name: "People Mover", location: "Tomorrowland", park: "Magic Kingdom"),
]

I've created a test view and called the "location" and "park" entries through ForEach loops and the correct data is showing up, indicating it's being written to core data successfully. It's just the relationship when retrieving that's broken.

I've also tried creating manual codegen NSManagedObject Subclass, like this video suggests, which I've watched so many times I feel like I know Paul Hudson and his dogs really well.

Any help solving this one is appreciated!


Solution

  • Thanks to the comments, I figured out the issue. I'm posting the solution in case it may help others.

    I was making this way more complicated than it needed to be. Turns out, I didn't need 2 Entities with a relationship between them. 1 Entity works just fine, since it's just housing additional details and I'm only sorting by date.

    I think the takeaway here is, ask yourself if you really need 2 Entities to be begin with.