티스토리 뷰

xcode 13.2 Beat에 iOS13 이상부터 Swift concurrency (async, await) 기능을 사용가능합니다.

완전히 좋은 기능입니다.

행복하죠. 콜백지옥에서 벗어날수 있습니다.

 

그럼 바로 변경하는 방법으로 넘어가겠습니다.

withCheckedThrowingContinuation, withCheckedContinuation 를 이용하는 방법입니다.

 

다음과 같은 closure를 사용하는 곳이 있었다고 합시다.

 

func testFunction(number: Int, handler: (Int) -> Void) {
  let sum = number + 1
  handler(sum)
}

testFunction(number: 1) { value in
  print(value)
}

결과는 2가 나옵니다.

 

그럼 다음과 같이 고쳐봅시다.

withCheckedThrowingContinuation을 이용합니다.

func testContinuation(number: Int) async throws -> Int {
  try await withCheckedThrowingContinuation { continuation in
    if number == 0 {
      continuation.resume(throwing: NSError(domain: "Sorry", code: -1))
    } else {
      let sum = number + 1
      continuation.resume(returning: sum)
    }
    
    // 이렇게 보낼수도 있습니다. 한꺼번에 나오는 케이스들도 가능
    // continuation.resume(with: Result<Int, Error>)
  }
}

Task {
  let sum1 = try? await testContinuation(number: 0)
  print(sum1) // nil
  
  let sum2 = try? await testContinuation(number: 1)
  print(sum2) // Optional(2)
}

closure를 만들지 않아도 되고

만약에 클래스에서 만들어졌다면 캡처가 발생하고

그렇게 되면 스레드가 전환되면서 비용이 많이 발생하지도 않고

코드가 우리가 보기도 훨씬 더 좋아졌습니다.

( 사실 한개만 해서 그렇지 비동기로 계속 호출하게 된다면 콜백 지옥에 빠져서 나오질 못합니다. )

사실 이 방법은 throws를 하는 경우에 대해서 만들어주는 방법입니다.

 

그럼 throws가 발생하지 않을 경우에는 어떻게 하면 될까요?

func testContinuation2(number: Int) async -> Int {
  await withCheckedContinuation { continuation in
    let sum = number + 1
    continuation.resume(returning: sum)
  }
}

Task {
  let sum1 = await testContinuation2(number: 0)
  print(sum1) // 1
  
  let sum2 = await testContinuation2(number: 1)
  print(sum2) // 2
}

그런데 continuation 에 같은 function을 가지고 있는데 특이한 부분은 Never 입니다.

절대로 안 일어난다는거죠.

나머지는 위의 withCheckedThrowingContinuation 하고 같지만 Error만 던지지 못합니다.

 

그럼 간결하게 사용할 수 있는 방법을 잘 찾아봐요.

안녕

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   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
글 보관함