https://kotlinlang.org logo
y

yshrsmz

12/04/2019, 10:44 AM
https://github.com/JetBrains/kotlinconf-app/blob/d56c3383d52dfdc32ba449d8daf9b9f633dd1d08/iosApp/iosApp/Controllers/HomeController.swift While looking at the KotlinConf app's code, I see iOS does not handle Closeables returned from CFlow. I wonder why.
CFlow looks nice btw
c

clhols

12/09/2019, 9:38 PM
I saw the same thing and also wondered that. Because CFlow starts "collecting" from the original flow on behalf of the UI, and passes back the "Job" in form of a Closable so that he UI can cancel the collecting coroutine when it doesn't want to get more values. But where do you clean up stuff in a UIViewController? viewDidUnload doesn't exist anymore...
I am trying to use the same concept from CFlow in a ViewModel that implements CoroutineScope. A function to observe a dataFlow: Flow<Data> would then be:
fun observeData(callback: (List<Data>) -> Unit) : Cancelable {
val job = launch {
dataFlow.collect { data ->
callback(data)
}
}
return object : Cancelable {
override fun cancel() {
job.cancel()
}
}
}
Cancelable is just an interface with fun cancel()
2 Views