Skip to content

Instantly share code, notes, and snippets.

@ioKun
ioKun / corutine_extract_callback.kt
Last active September 11, 2018 06:11
Wrapping java listener with kotlin corutine to return List of data in suspend way
// Wrapper funtion for listener/callback
suspend fun hiSearchCameraLan(): Channel<HiSearchSDK.HiSearchResult> {
val channel = Channel<HiSearchSDK.HiSearchResult>(Channel.UNLIMITED)
launch(UI) {
val searchSDK = HiSearchSDK(HiSearchSDK.ISearchResult { result ->
if (!result.uid.isEmpty())
channel.offer(result)
else
channel.close(NullPointerException())
})