UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

CoreData and Sections

Forums > SwiftUI

I have an app I've been toying with for a few years. It consists of a one to many relationship between a person and many events. I tried to follow Paul's video One to Many relationships to setup my desired view, but my data is not working for it. I am trying to figure out how best to approach solving the following problem:

I would like to have one view that shows all the people, you select the person and then you get a list of all that person's events (this is my default behavior and it works fine). The second view would be a list of all the types of events, you would select an event and get a list of all the people who have that event, sorted by person and then date.

The challenge is, my events entity consists of the event name, event date, a picture of the event, a reference to the person who the event belongs to and a unique UUID.

Each person entity has a NSSet of events, along with a name, and unique UUID.

Based on the above video I created a property that is a SET:

public var eventArray: [Event] {
   let set = events as? Set<Event> ?? []

   return set.sorted {
     $0.wrappedEvent < $1.wrappedEvent
   }
}

When I create the sections in my list view

ForEach(events, id: \.self) { event in
       Section(event.wrappedEvent) {
          ForEach(event.recipient?.eventArray ?? [], id: \.self) { eventName in
              NavigationLink(destination:
                        ViewAnEventView(event: event, recipient: event.recipient!)) {
                                Text("\(eventName.wrappedEvent) \(eventName.wrappedEventDate, formatter: FilteredList.eventDateFormatter)")
                           }
           }
       }
 }

I end up with a Section that shows up multiple times per event and the values inside each section, is actually a list of events for a person. It actually appears to me that the outer ForEach is all the persons (with one section per event each person has), and then inside each section I get all the events for that person. Definitely not the desired results, any pointers would be greatly appreciate. (I can share my git repo if it helps).

1      

Hi there! May I just clarify your request? You would like to have a list of people and by tapping show all the events for that person, Then by tapping an event you want to show all people assigned for that event? It seems like you want have Person Entity with To Many relationship to events and Event Entity with To Many relationship to persons, but currently you have the latter set as To One...

1      

@ygeras not quite... The ability to show all the events a person has is not a problem. What I want is to get a distinct/unique list of all the events (by name), and then show a list of all the people who have that event. (I fear that the data is not setup correctly for this).

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Maybe these two pictures will show what I mean - the default view is By Person and this is what happens with my current by event View.

The second view should be A section called Anniversary and another called Birthday, with John Appleseed being in the first one, and both John Appleseed and Kate Bell being in the second one.

1      

That is what I meant, for that you have to have To Many relationship. Person can have many events and event can have many people. You need to change the relationship. Currently you have person has many events and event has only one person.

UPD then you need separate event types judging by screenshots

1      

yeah, I was afraid of that, I have tons of data, that I would need to right a conversion utility for, in order to migrate to a seperate event type entity.

1      

I've never had to write a conversion utilty to run within CoreData, since this is my first app using Core Data. Also, any prior changes all where simple changes (adding a column or removing a column). Are there any good tutorial's on how to do a CoreData migration? I am thinking that structurally, I need to read all the existing EVENTS and create a new object that is a unqiue set of EventTypes. Then I would need to change the Events to point at that Event Type, with a one to Many coming back, i.e. each event Type can have many different Events (dates, pictures, etc.).

1      

Recently BigMountainStudio released a book on CoreData. They have a section on migration. https://www.bigmountainstudio.com/core-data also Practical Core Data of Donny Walls touches more advanced topics on migration https://donnywals.gumroad.com/l/practical-core-data. But they are not free :). As for online tutorials, maybe there are some. But I preferred to have those books. PS. I am not associated with authors in any way, so do not consider it as an ad, just recommendation.

2      

hi Michael,

I've never had to write a conversion utilty to run within CoreData

and you probably will not have to do this yourself, since Core Data can figure out how to update a to-one relationship to be a to-many relationship when it migrates from one version to another.

WWDC 2022 had a talk named "Evolve your Core Data schema" that may be of help (as well as the resources mentioned by @ygeras).

hope that's of use,

DMG

2      

Thank you both @ygeras and @delawaremathguy - Will check out the related resources. As my wife and I have become very dependant on the data in my little (unpublished) app, I can't risk losing the data. (oh did I mention, it also syncs to CloudKit!!, so I really want to get this right).

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.