|
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:
When I create the sections in my list view
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). |
|
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... |
|
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. 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 and this is what happens with my current 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. |
|
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 |
|
|
|
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.). |
|
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. |
|
hi Michael,
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 |
|
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). |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.