Schadenfreude
05/02/2019, 2:54 PMAuthorizationManager.getToken(context, SCOPES, tokenListener)
I want to wait for it to get the result in the listener and then continue on with my code but I can’t figure out if it’s at all possible.streetsofboston
05/02/2019, 2:59 PMsuspend fun getAuthManagerToken(context: Context, scopes: Scopes) : Token = suspendCoroutine { cont ->
AuthorizationManager.getToken(context, scopes) { token ->
cont.resume(token)
}
}Schadenfreude
05/02/2019, 3:03 PMListener<AuthorizeResult, AuthError>streetsofboston
05/02/2019, 3:05 PMAuthError a Throwable?louiscad
05/02/2019, 3:05 PMresumeWithException when there's an error, and if there's no Throwable, use your ownSchadenfreude
05/02/2019, 3:06 PMstreetsofboston
05/02/2019, 3:06 PMsuspend fun getAuthManagerToken(context: Context, scopes: Scopes) : Token = suspendCoroutine { cont ->
AuthorizationManager.getToken(context, scopes) { token, error ->
if (error != null) resumeWithException(error)
else cont.resume(token)
}
}streetsofboston
05/02/2019, 3:07 PMSchadenfreude
05/02/2019, 3:33 PMprivate suspend fun getAuthManagerToken(context: Context, scopes: Array<Scope>): AuthorizeResult =
suspendCoroutine { cont ->
AuthorizationManager.getToken(context, scopes, object : Listener<AuthorizeResult, AuthError> {
override fun onError(authError: AuthError) {
cont.resumeWithException(authError)
}
override fun onSuccess(authResult: AuthorizeResult) {
cont.resume(authResult)
}
})
}Sam
05/02/2019, 8:05 PMstreetsofboston
05/02/2019, 8:10 PMgetAuthManagerToken has no reason to cancel the suspended coroutine itself.
The outer-scope that calls getAuthManagerToken still can cancel the suspended fun.
(suspendCancellableCoroutine will just provide a cont to the implementation of getAuthManagerToken that now has an additional cancel function.)Sam
05/02/2019, 8:13 PMSam
05/02/2019, 8:14 PMstreetsofboston
05/02/2019, 8:21 PMgetAuthManager doesn’t need to worry about it in this case.
If the getAuthManagerToken itself needs to cancel it, then it needs to call suspendCancellableCoroutine so that it has a CancelableContinuation (not sure if I have the class-name right) instead of a plain Continuation and now it can call cancel() on it, if necessary, instead of calling resumeWithException(...).
Cancelation is almost(!!) like resumeWithException, because it cancels the suspending call, but it is not ‘fatal’, it has just canceled.pakoito
05/02/2019, 11:38 PMlouiscad
05/02/2019, 11:47 PM