Thoughts on sorting events
Objective:
I need a way to sort events in a list view that makes them easy to find.
Constrains:
- All events have a start date
- Some events have an end date
- Some people may look for an event based on its start date, other may want to look for the end date. It could even vary depending on the type of data tracked on the timeline.
Options:
- Show only a list of events sorted by start date. Not at all what I want.
- Add a timeline setting to control sorting all related events. In this option an event would appear once in a list, either at the start date or end date position.
- Add a Bool to the Timeline entity to indicate preference to use end date if available.
- If an event has an end date, use it for the sorting
- Else, sort by start date
- Generate a list of all events with all start dates AND a list of events with end dates.
- Start date list – all events for a timeline
- End date list – only events with an end date for a timeline
- Combine these lists together and sort them. I don’t know how to sort both lists together like this…
I’d like to proceed with option three from the list above. Using this approach will place an event in the list twice if it has a start and end date. I can format the rows in the list with some indication of type such as single date, start date range, end date range. I can find some symbols to indicate what type the are. I could also place a toggle switch on the list layout to quickly show and hide the end date rows. I might even be able to make a cool animation for this.
I’m running into problems with how to actually pull this off. I wrote a simplified version of this in a playground but I can’t figure out how to sort the final resulting array, as the elements in the array have different sorting rules depending on rather they are a starting or ending record.
var fetchResults = [Event] // this is populated with an array of events
var endDateRows = fetchResults.filter({$0.includeEndDate == true && $0.dateEnd != nil})
var rows = fetchResults + endDateRows // combine the original array with the new array
// Now what do I sort by???
I’m considering some possible solutions but I’m not sure if these are a good way to proceed.
Solution A.
Make a View Model that can fetch all of the events from Core Data, then process them into a new array of a different type. I could make a new containing object like this.
class EventContainer {
var dateToSort: Date
var eventRecord: Event // a reference to the Core Data record
}
My View Model could make two arrays of EventContainer, using map and filter calls to write the start and end dates to the dateToSort field, and writing the entire event to the eventRecord field. Then it could combine these two arrays into one and sort by the dateToSort field.
This approach seems reasonable but it puts a lot of processing into the hands of the View Model. Every time I navigate to a list I’ll have to run these processes. For individual Timelines that may not be a problem but when it comes to making list views that show events from all Timelines this could run into performance issues.
Solution B.
Modify the schema. Currently I have a simple Event entity in Core Data. In simplified form it looks like this.
class Event {
var name: String
var dateStart: Date
var includeDateEnd: Bool = false
var dateEnd: Date?
}
I could modify this by moving the date fields into a new table and making one to one relationships.
class Event {
var name: String
var dateStart: EventDate // Core Data To One relationship
var includeDateEnd: Bool = false
var dateEnd: EventDate //Core Data To One relationship
}
class EventDate {
var date: Date
}
If I set this up correctly I could modify my fetch requests to record a list of EventDate records, then use the relationships to access the rest of the event data. This option would probably perform far better. It’s also a ton of work to update the app as it stands now. This might also complicate some of the calculations involving dates because the will be several database records involved instead of one Event record.
Out of these two options I’m leaning towards the first. This is not really an approach I’ve taken before and I think it could be valuable to try it. Coming from 8 years of FileMaker development, my first thought is to try to solve this like a database normalization problem, but I’ve learned the hard way before that Core Data is not a database. Sure, it uses a data and fills the same role as a database, but it’s an Object Modeling tool. It may be good to force myself to use a new type of thinking to solve this problem. If I run into performance issues then I can also take another crack at the second option. Either way, I need to spend some time thinking about this more before I decide how to proceed. Perhaps this is a good topics for Project Update.