티스토리 뷰

728x90
반응형

내용

  • MainActor 를 사용해서 main thread 에서의 동작을 보장해보자.

MainActor 는 Concurrency 의 Actors API collection 중 하나입니다.

Apple Developer Documentation

MainActor

Apple Developer Documentation

A singleton actor whose executor is equivalent to the main dispatch queue.

즉, mian thread 에서의 동작을 보장하는 Actor 입니다.MainActor 를 사용하면 DispatchQueue.main 을 언제 사용할지 고민하지 않아도 됩니다!

Task {
    do {
        movies = try await getMovie()
        await MainActor.run {
            movieCollectionView.reloadData()
        }
    } catch {
        // error handling.
    }
}

DispatchQueue.main 과 동일하게 다음 run loop 를 기다리지 않고 비동기적으로 처리합니다.

출처:

Apple Developer Documentation

Protect mutable state with Swift actors

How to use @MainActor to run code on the main queue

728x90
반응형
댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
링크
Total
Today
Yesterday