[SwiftUI] .task()에서 throwing function 사용 시 오류

zxxhe·2023년 2월 4일
0
post-thumbnail

Invalid conversion from throwing function of type '@Sendable () async throws -> Void' to non-throwing function type '@Sendable () async -> Void’

문제 상황

async throws를 사용한 함수를 .task() 안에 사용하려고 했다.


해결 과정

처음에는 .task() 안에 do-try-catch문을 사용하면 안되는건가 싶어서 .task() 대신에 .onAppear() 안에 넣어 봤다.

Invalid conversion from 'async' function of type '() async throws -> Void' to synchronous function type '() -> Void’

응 안 되는거 똑같아 ㅋ 당연하지 onAppear 만으로는 우선 await를 사용하지 못하니까
.
.
.

함수에서 do-try-catch, throw 관련된 코드를 계속 수정해봤지만 에러는 그대로...
그러다 이 스택오버플로우를 발견했다.
https://stackoverflow.com/questions/75225934/task-works-but-task-throws-invalid-conversion-from-throwing-function-of-typ

stackoverflow 요약 : .task()는 catch 블록에서 throw를 해주면 안된다.


결론

  1. .onAppear(Task()) 를 사용해라.

  2. .task() 안에서 catch문의 throw(error) 를 하지 말자.

profile
iOS / SwiftUI

0개의 댓글