Post

Replies

Boosts

Views

Activity

Reply to Struggling with async/await: Fetching an image off the main thread
Hey Quinn, @DTS Engineer My bad—initially, I wrote the entire code in the view controller to simplify things (yes, I know, Massive View Controller is not a great practice! LOL). I’ve now restructured the code using a simple VIPER architecture. The Actual Goal: I want to fetch data from a server off the main thread, including any data conversion needed for the UI, and then update the UI before and after the fetch. How I Structured It: • The dataTask is triggered inside the interactor’s async fetchListData() function. • In the presenter (which is a class, not an actor), I call fetchListData() using Task. However, since Task doesn’t inherit an actor context, after an await suspension point, it might resume on a different thread. To handle this, I annotated my presenter method with @MainActor to ensure UI updates happen on the main thread. My Questions: Is using @MainActor in the Task the best practice? Or should I make my entire presenter a MainActor? If I use @MainActor in the presenter, will the interactor’s fetchListData() always be executed off the main thread? I don’t want to overload the main thread with network calls or data conversion. Is there a way to ensure that the interactor always returns values on the main thread so that I don’t need to rely on @MainActor annotations in the presenter? Git repo : https://github.com/praveeniroh/AsynAwaitTest
3w