Home > Enterprise >  Add multiple events to the Calendar with Concurrency
Add multiple events to the Calendar with Concurrency

Time:12-14

I created a function to add my course events to the calendar app using EventKit.

After learning the swift concurrency, I want to update my code to make the progress much faster, namely using the detached task or TaskGroup to add these events.

Synchronize code without detached task or task group:

func export_test() {
    Task.detached {
        for i in 0...15 {
            print("Task \(i): Start")
            let courseEvent = EKEvent(eventStore: eventStore)
            courseEvent.title = "TEST"
            courseEvent.location = "TEST LOC"
            courseEvent.startDate = .now
            courseEvent.endDate = .now.addingTimeInterval(3600)
            courseEvent.calendar = eventStore.defaultCalendarForNewEvents
            courseEvent.addRecurrenceRule(EKRecurrenceRule(recurrenceWith: .daily, interval: 1, end: nil))
            do {
                try eventStore.save(courseEvent, span: .futureEvents)
            } catch { print(error.localizedDescription) }
            
            print("Task \(i): Finished")
        }
    }
}

Doing the same thing using the TaskGroup :

func export_test() {
    Task.detached {
        await withTaskGroup(of: Void.self) { group in
            for i in 0...15 {
                group.addTask {
                    print("Task \(i): Start")
                    let courseEvent = EKEvent(eventStore: eventStore)
                    courseEvent.title = "TEST"
                    courseEvent.location = "TEST LOC"
                    courseEvent.startDate = .now
                    courseEvent.endDate = .now.addingTimeInterval(3600)
                    courseEvent.calendar = eventStore.defaultCalendarForNewEvents
                    courseEvent.addRecurrenceRule(EKRecurrenceRule(recurrenceWith: .daily, interval: 1, end: nil))
                    do {
                        try eventStore.save(courseEvent, span: .futureEvents)
                    } catch { print(error.localizedDescription) }
                    
                    print("Task \(i): Finished")
                }
            }
        }
    }
}

The output of the TaskGroup version:

Task 0: Start
Task 1: Start
Task 2: Start
Task 4: Start
Task 3: Start
Task 5: Start
Task 6: Start
Task 7: Start
Task 0: Finished
Task 8: Start
Task 1: Finished
Task 9: Start

Sometimes, only a few tasks will been done, and others will not, or even never been started (I created 16 tasks but only printed 9 in this example). Sometimes, all of these events can be added.

In my point of view, I have created 16 child tasks in the TaskGroup.

Each child task will add one event to the Calendar. I think in this way, I can take the full advantage of the multi-core performance (maybe it's actually not.

  • Related