코루틴을 공부하다가 의문점이 생겼다.
해리님의 코드를 보면
이렇게 짜여져 있고, awaitClose()가 close가 호출될때까지 기다린다고 돼있다.
if (response.isSuccessful) {
response.body()!!.let { offer(it) }
}
하지만 이 코드가 실행된다면 close가 없는데
그렇다면 awaitClose는 실행되지 않는걸까?
해당 의문점을 해결하기 위해 offer에 대해 찾아보았더니,
내가 제대로 이해한게 맞다면, offer가 아래 코드로 migration 된다고 한다.
NB Automatic migration provides best-effort for the user experience, but requires removal or adjusting of the code that relied on the exception handling. The complete replacement has a more verbose form:
channel.trySend(element)
.onClosed { throw it ?: ClosedSendChannelException("Channel was closed normally") }
.isSuccess
그럼 여기에 onClosed가 있는데, 결국 offer를 쓰면 close가 되서 awaitClose()가 실행되는 건가?
근데 onClosed가 뭐지??
아직도 정확한 내용을 모르겠다.
혹시 조언을 주실 분 계신다면 댓글 남겨주시면,, 감사하겠습니다.
역시 어려운 코루틴!
---------------- 내용 추가
해당 공식문서를 따르면
https://developer.android.com/kotlin/flow
awaitClose()가 closed나 cancelled 됐을때 실행된다고 하고,
하단에 offer가 즉각 false를 return 한다고 나와 있다.
whereas offer does not add the element to the channel and returns false immediately.
그럼 여기서 return이 코루틴을 cancelled 시키는 걸까??
새로운 의문점 추가... +1
---------------- 내용 추가2
https://stackoverflow.com/q/68217043/14299073
결국 질문글을 올렸다!
이렇게.. 질문하는게 맞겠지?
+ 답변이 달렸다!