Allan Wang
12/21/2018, 12:17 AMdoA {
doB {
doC()
}
}
And I want to make them all in the same level, would it be done like the following?
// Original
fun doA(continuation: () -> Unit) {
// some action
continuation()
}
// New
suspend fun doA() = suspendCoroutine<Unit> {
// some action
it.resume(Unit)
}
Then I do something like
launch {
doA()
doB()
doC()
}
Which should occur one after the other by default.
My goal is just to remove the nested brackets. The reason I have continuation to begin with is that the actions I’m running deal with callbacks.
Is there also a benefit to doing some potential safety checks before calling suspendCoroutine vs putting them all inside that wrapper? The delay function seems to do that.gildor
12/21/2018, 12:26 AMAllan Wang
12/21/2018, 12:30 AMprivate fun setWebCookie(cookie: String?, callback: (() -> Unit)?) {
with(CookieManager.getInstance()) {
removeAllCookies { _ ->
if (cookie == null) {
callback?.invoke()
return@removeAllCookies
}
L.d { "Setting cookie" }
val cookies = cookie.split(";").map { Pair(it, SingleSubject.create<Boolean>()) }
cookies.forEach { (cookie, callback) -> setCookie(FB_URL_BASE, cookie) { callback.onSuccess(it) } }
Observable.zip<Boolean, Unit>(cookies.map { (_, callback) -> callback.toObservable() }) {}
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
callback?.invoke()
L.d { "Cookies set" }
L._d { cookie }
flush()
}
}
}
}
In android, remove all cookies and set cookies all take callbacks. I want to clear all cookies and then get set a bunch of cookies, before performing actions. All the other methods are similar.Dico
12/21/2018, 12:44 AMdoA
, doB
and doC
with suspend
function and remove the callback parametersgildor
12/21/2018, 2:49 AMsuspend fun CookieManager.removeAllCookiesSuspend(): Boolean {
return suspendCoroutine { cont ->
removeAllCookies {
cont.resume(it)
}
}
}