I would like to understand if it's needed to declare the Task itself as MainActor considering the following block of code.
func login() {
Task { [weak self] in
let result = await self?.loginService.start()
if result == .successful {
self?.showWelcomeMessage() // updating the UI here
}
}
}
final class LoginService {
@MainActor
func start() async -> LoginResult {
// Doing some UI related operations here
}
}
Do I need to declare the Task itself as MainActor too when the async function which is called inside the Task is declared as MainActor already? Like this:
func login() {
Task { @MainActor [weak self] in
let result = await self?.loginService.start()
if result == .successful {
self?.showWelcomeMessage() // updating the UI here
}
}
}
I believe if a Task itself is declared as MainActor, child async operations will inherit the parents configuration if not declared otherwise, but does it also work the other way around?