Skip to content
vrhermit

vrhermit

Code & Writing by Joseph Simpson

  • Canvatorium
  • Technical
  • Professional
  • Personal
vrhermit
vrhermit
Code & Writing by Joseph Simpson
Retrospective | Technical

Follow up to event sorting

ByJoseph September 26, 2019June 25, 2023
CoreData

This is a short follow up article regarding the problems I described in this post.

I spent some time mocking this up in a Swift Playground on my iPad. First I added a new container object called EventContainer. This is an object that I can map my events to, while keeping a reference to the Core Data Event record.


class EventContainer {
    enum EventType {
        case startEvent
        case endEvent
    }
    var date: Date
    var eventType: EventType
    var event: Event
    
    init(date: Date, eventType: EventType, event: Event) {
        self.date = date
        self.eventType = eventType
        self.event = event
    }
}

I started by mapping the entire list of fetched events to an array of EventContainer

let startDateRows: [EventContainer] = fetchResults.compactMap ( { EventContainer(date: $0.dateStart!, eventType: .startEvent, event: $0 )} )

Next I filtered the fetched events to just those with end date data

let endDataData = fetchResults.filter({$0.includeEndDate == true && $0.dateEnd != nil})
let endDateRows: [EventContainer] = endDataData.compactMap( { EventContainer(date: $0.dateEnd!, eventType: .endEvent, event: $0 ) } )

Lastly I combined these arrays into one new array and sorted it based on the date property from the EventContainer

let fullList = startDateRows + endDateRows
fullList.sorted(by: {
    $0.date.compare($1.date) == .orderedDescending
})

Now I have a sorted array of EventContainers that I can use to build my list UI. Each list row can still access the Core Data Event entity to pass on to editing views. I think this is a reasonable approach, but I’m really just guessing. If you know a better way to do this please get in touch using the contact form or contact me on Twitter.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Tumblr (Opens in new window) Tumblr
  • More
  • Click to print (Opens in new window) Print
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp

Like this:

Like Loading...
Post Tags: #CoreData
Next: Event row types (WIP)
Previous: Thoughts on sorting events

Get my articles in your email

Join 29 other subscribers

A-Frame AI AppUpdate AR BabylonJS Books Career ChatGPT CloudKit CoreData FileMaker Food Gaming Kadence MixedReality parody PlayCanvas Podcast SpatialComputing SwiftUI Thoughts visionOS VisionPro VR VueJS WebXR WordPress

Work with Me

Ready to streamline your workflows and enhance your digital presence?

Do you want to take your first step into Spatial Computing?

Discover how Radical Application Development can help transform your business.

Get in touch or learn more at radicalappdev.com

Mastodon Mastodon
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy

Follow my work

Github Linkedin YouTube

© 2025 Joseph Simpson | Radical Application Development

  • Canvatorium
  • Technical
  • Professional
  • Personal
Search
%d