Profile

성빈이의 기록장

성빈이

[Coroutine] offer는 코루틴을 close/cancelled 시키는 걸까?

728x90

코루틴을 공부하다가 의문점이 생겼다.

 

https://medium.com/harrythegreat/kotlin-retrofit%EC%97%90-flow%EC%99%80-callbackflow-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0-f7797db754c6

 

[Kotlin] Retrofit에 Flow와 CallbackFlow 적용하기

본 예제는 Okhttp와 Retrofit에 대한 튜토리얼 내용은 설명하지 않습니다.

medium.com

 

해리님의 코드를 보면

이렇게 짜여져 있고, awaitClose()close가 호출될때까지 기다린다고 돼있다.

 

if (response.isSuccessful) {
	response.body()!!.let { offer(it) } 
}

 

하지만 이 코드가 실행된다면 close가 없는데

그렇다면 awaitClose는 실행되지 않는걸까?

 

 

해당 의문점을 해결하기 위해 offer에 대해 찾아보았더니,

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-send-channel/offer.html

 

내가 제대로 이해한게 맞다면, 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()closedcancelled 됐을때 실행된다고 하고,

 

하단에 offer가 즉각 falsereturn 한다고 나와 있다.

whereas offer does not add the element to the channel and returns false immediately.

 

그럼 여기서 return이 코루틴을 cancelled 시키는 걸까??

 

새로운 의문점 추가... +1

 

 

 

---------------- 내용 추가2

 

 

 

https://stackoverflow.com/q/68217043/14299073

 

Does offer make the coroutines close or canceled?

I was looking at the flow documentation on the Android Developer site and I have a question. https://developer.android.com/kotlin/flow#callback If you look at the above link, you will see code like...

stackoverflow.com

결국 질문글을 올렸다!

이렇게.. 질문하는게 맞겠지?

 

 

 

+ 답변이 달렸다!

https://stackoverflow.com/a/68217560/14299073

 

Does offer make the coroutines close or cancelled?

I was looking at the flow documentation on the Android Developer site and I have a question. https://developer.android.com/kotlin/flow#callback If you look at the above link, you will see code like...

stackoverflow.com