curioustechizen
03/22/2019, 12:45 PMsuspendCancellableCoroutine
. We're looking for ways to signal progress in addition to completion and error.
Currently, we pass a channel as a parameter to the suspending function through which we signal progress (this is conceptually similar to having a callback for progress). Are there other options? Like using the continuation object to signal progress?streetsofboston
03/22/2019, 12:47 PMContinuation
can be continued only once. It is like an Rx Single or Maybe. For multiple (more than one) 'callbacks', using a Channel is your best bet.Dico
03/22/2019, 12:49 PMsetProgress
function. You can wrap a conflated channel with it if you want, that would make sense.interface ProgressCallback {
fun setProgress(value: Double)
fun delegateProgress(portion: Double): ProgressCallback
}
inline fun ProgressCallback.delegateWork(portion: Double, block: ProgressCallback.() -> Unit) = delegateProgress(portion).block()
curioustechizen
03/22/2019, 1:03 PMsuspend fun downloadSomething() = suspendCancelableCoroutine<Result> {continuation->
val listener = object: Listener{
override fun onDownload(result: Result) = continuation.resume(result)
override fun onProgress(percent: Int) {
//Signal download progress here
}
override fun onFailure(e: Exception) = continuation.resumeWithException(e)
}
api.addListener(listener)
api.startDownload(thing)
continuation.invokeOnCancellation {
api.cancelDownload()
api.removeListener(listener)
}
}