10

I load books from API, show activity indicator while loading, update label after server response.

activityView.isHidden = false
        
let task = detach {
    do {
        let books = try await self.bookService.fetchBooks()
        DispatchQueue.main.async {
            self.show(books: books)
        }
    } catch {
        DispatchQueue.main.async {
            self.resultLabel.text = error.localizedDescription
        }
    }
    DispatchQueue.main.async {
       self.activityView.isHidden = true
    }
}

//...

My question is what is better approach to update UI on the main queue? DispatchQueue.main.async look ugly and I guess there is a better approach to do the same.

I must use it, because all UI updates should be on the main thread and I get compiler errors without DispatchQueue.main.async something like

Property 'text' isolated to global actor 'MainActor' can not be mutated from a non-isolated context

or

Property 'isHidden' isolated to global actor 'MainActor' can not be mutated from a non-isolated context

P.S. Use Xcode 13.0b2

Ivan Vavilov
  • 1,520
  • 1
  • 15
  • 28

1 Answers1

11

Use @MainActor like this -

self.updateAcitivityIndicator(isHidden: false)
        
let task = detach {
    do {
        let books = try await self.bookService.fetchBooks()
        self.showBooks(books)
    } catch {
        self.showError(error)
    }
    self.updateAcitivityIndicator(isHidden: true)
}

@MainActor
private func showBooks(_ books: [Book]) {
}

@MainActor
private func showError(_ error: Error) {
    self.resultLabel.text = error.localizedDescription
}

@MainActor
private func updateAcitivityIndicator(isHidden: Bool) {
    self.activityView.isHidden = isHidden
}
Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30
  • 1
    Hey Tarun, What is `@MainActor`? i don't have knowledge about it so can you please tell me. – jatin fl Jul 05 '21 at 08:24
  • 1
    @jatinfl See [`MainActor`](https://developer.apple.com/documentation/swift/mainactor) - `A singleton actor whose executor is equivalent to the main dispatch queue.` – Tarun Tyagi Jul 05 '21 at 08:30
  • 1
    so while working with `@MainActor` we don't need to play with `GCD` or `OprationQueue`. Right? – jatin fl Jul 05 '21 at 08:32
  • 2
    @jatinfl These are different concepts, `@MainActor` was introduced with `async` / `await` for this very purpose OP needs to resolve. Please watch **WWDC 2021** sessions related to Swift Concurrency and you will know what you need. – Tarun Tyagi Jul 05 '21 at 08:36