Skip to main content

SwiftUI View-body의 MainActor와 커스텀뷰

Updated Markdown
Part of seriesSwiftUI

SwiftUI의 View - body는 기본적으로 @ MainActor 매크로를 채택하고 있다.

하지만 여기서 문제

body안에 내가 만든 커스텀 some View를 넣으면 어떻게 될까?

struct ExampleView: View {
    var body: some View {
        CustomView
    }

    var CustomView: some View {
        Button(action: {
            Task {
                print(Thread.isMainThread)
            }
        }) {
            Text("버튼입니다.")
        }
    }
}

내가 생각 했던 가정

MainActor 에서 CustomView 실행시키니깐 MainActor를 채택할 것이다.

정답은 ❌false 가 출력된다

💡
body를 나가는 순간 @ MainActor 속성을 잃게 된다.

About the author

  • 안녕하세요🙇🏻‍♂️ 세상을 더 편리하게 바꾸고 싶은 iOS 개발자 최인호입니다.

    Hello 👋 I'm Inho Choi, an iOS developer who wants to change the world more conveniently.

    • 대학교 졸업
    • Apple Developer Academy @ POSTECH 1기
    • KWDC Main Organizer
    • AsyncSwift Organizer

No comments yet. Be the first to comment.

SwiftUI

Part 1 of 1

스유 관련된 공부 적는 곳

More from this blog