Reload decorator UI CalendarView with async data

Hi, I don't have too much experience with UIKit I'm trying to reload the decorators for my calendar but I can't find a way to do it.

I have a view Model that gets the events by using async example:


@Published var events: [EventSession] = []
@MainActor func getEvents() async {
        self.events = [
            EventSession(
                eventId: "1",
                sessionId: "1",
                userId: "1",
                title: "Event 1",
                startDate: Date(),
                sessionStatus: "ACTIVE",
                eventStatus: "ACTIVE",
                isPrincipalSession: true,
                users: ["1"]
            ),
            EventSession(
                eventId: "2",
                sessionId: "2",
                userId: "2",
                title: "Event 2",
                startDate: Calendar.current.date(byAdding: .day, value: 2, to: Date())!,
                sessionStatus: "ACTIVE",
                eventStatus: "ACTIVE",
                isPrincipalSession: true,
                users: ["2"]
            ),

In my SwifUI View I'm invoking my CalendarViewRepresentable

 VStack {
            ScrollView(showsIndicators: false) {
                CalendarViewRepresentable(events: $vm.events, dateSelected: $dateSelected, displayEvents: $displayEvents, interval: DateInterval(start: .distantPast, end: .distantFuture))
                    .tint(.accentColor)
                
                DayEventsView(dateSelected: $dateSelected)
                    .environmentObject(vm)
            }
        }
        .navigationTitle("Calendar")
        .navigationBarTitleDisplayMode(.inline)
        .onAppear {
            Task {
                await vm.getEvents()
            }
        }

And in my CalendarViewRepresentable this is my coordinator

class Coordinator: NSObject, UICalendarViewDelegate, UICalendarSelectionSingleDateDelegate {
        
        var parent: CalendarViewRepresentable
        @Binding var events: [Event]
        @Binding var dateSelected: DateComponents
        
        init(parent: CalendarViewRepresentable, events: Binding<[Event]>, dateSelected: Binding<DateComponents>) {
            self.parent = parent
            self._events = events
            self._dateSelected = dateSelected
        }
        
        @MainActor func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
            let foundEvents = events.filter { event in
                event.sessions.contains { session in
                    session.startDate.startOfDay == dateComponents.date?.startOfDay
                }
            }

            if foundEvents.isEmpty { return nil }
            if foundEvents.count > 1 { return .default(color: .darkGray) }

            return .default(color: .red)
        }
    }    

As I understand I should update this function

func updateUIView(_ uiView: UICalendarView, context: Context) {
    }

But each time I try to use context.coordinator.events = events it does not work because it says I'm publishing change, not in the main queue

or if i use this

DispatchQueue.main.async {
  context.coordinator.events = events
}

the decorators are also not refreshed.

I tried also running uiView.reloadDecorations(forDateComponents: [DateComponents], animated: true)

but the decorators are not reloaded

UICalendarView requires that you call reloadDecorations(forDateComponents:animated:) when your data changes in order to refresh your decorations. If things are not refreshing as expected, then please double check that the DateComponents you are passing into the reload function are correct.

Reload decorator UI CalendarView with async data
 
 
Q