Schema changes and date sorting
I wrote about a sorting issue in Retrospective Timeline a while back. You can read the full post here. At the time I decided to go with Solution A, which involved wrapping my core data objects in a container using map and filter calls. That approach worked at first, but there were some issues when it came to refreshing the SwiftUI views. Basically, since I was moving the data outside of the fetched results controller, the views could no longer receive publisher updates when data was changed. I could work around this but it felt like reinventing the wheel.
Last week I decided to modify the schema instead. I started with this
- Timeline (Parent object)
- Event (Child Object
- Start date value
- End date value
I added a new table called RTDate and moved the Event date fields here. The entities I have now are
- Timeline
- Event
- RTDate (the “RT” prefix is just to avoid confusion with Date types)
As for the relationship from Event to RTDate, I decided to go with two “To One” relationships rather than a “To Many”. As a database developer this isn’t what I would normally do, but it is important to keep in mind that I really just wanted to relocate some properties in a new table that can drive the UI.
I finished up the schema changes yesterday, ripping out the old version as I went. Both Core Data and CloudKit now have the new schema. I considering doing some sort of migration but decided against it since I’m the only user for now. It’s easier to just re-add my data when I’m ready.
This week I’m going to focus on adapting my views for this new schema. Most of what I have now will be easy to modify, but the list of events (now list of dates) will take some extra work. There are four types of date rows that can show up in this list view.
- Single – the date record for an event with a single date
- Start – the start date record for an event that has an open or closed date range
- End – the end date record for an event that has a close date range
- Ongoing – the end date record for an event that has an open range.
The “Ongoing” row type will take some extra work. This is a valid RTDate record, but the date property is set to nil
because no date is selected. I need to substitute the current date for these records. I think I can use Core Data Derived Attributes for this, but I’m having trouble learning how to use that feature.