ViewModel의 Published-Prop 값을 update 하는 과정에서
보라색 warning을 만났다.
↪ receive(on:) 를 보아하니 Combine Framework와 관련된 경고인것 같다.
(Combine 잘 머른다)
경고 내용은 대략
'UI 렌더링 및 이벤트 처리를 왜 Main-Thread가 아니라, Background-Thread에서 처리를 해주고 있느냐'
인 것 같다.
메서드를 호출해준 view로 넘어가보니
.onAppear {
Task {
do {
_ = try await modelData.fetchDataFromFirestoreWithAsyncAwait()
} catch {
print(error.localizedDescription)
}
}
}
fetchDataFromFirestoreWithAsyncAwait()를 Main-Thread에서 실행시켜주지 않고 있었다 🥲
근데, fetchDataFromFirestoreWithAsyncAwait() 얘는 async 키워드가 붙은 메서드인데 어떻게 Main-Thread에서 실행시켜주지? 🤔
(내가 아는 방법은 )DispatchQueue.main.async
이 방법 뿐인데,
이 안에서는 await로 메서드를 호출할 수가 없는뎅..
또 방법을 찾아보았다ㅏ
결론적으로 말하자면
@MainActor
func fetchDataFromFirestoreWithAsyncAwait async throws -> [Landmark] { }
메서드 정의부에 @MainActor 라는 Property-wrapper를 추가해주면 된다!
이 밖에 다양한 해결법이 존재하는것 같았다.
알아본 바로는
또는 receive(on:options:) 사용하기 였는데
무슨 이유에서인지는 잘 모르겠지만
Task.detached(priority:operation:)
와 MainActor.run(resultType:body:)
를 사용했을 때에도 똑같은 warning이 나왔고 (흐음ㅁ...)
receive(on:options:)
의 경우 아직 Combine을 잘 모르기에
해결해볼 수가 없었다.
현재는 @MainActor
Property-Wrapper를 사용해서 문제를 해결하였으나,
Combine 사용법을 익혀 receive(on:options:)
를 이용해 해결해보고
포스트를 수정해야겠다. 🔥
끝ㅌ!