Search code examples
google-play-services

How to run new gms Task object synchronously like PendingResult.await?


I don't see any await synchronous api on com.google.android.gms.tasks.Task. Am I missing something? I am trying to migrate to use the new *Client classes in Play services. I already designed my code to run in another thread and use PendingResult.await. My code was like this:

val pendingResult = Auth.GoogleSignInApi.silentSignIn(TwinkleApplication.instance.gapiClient)
val account = pendingResult.await(10, TimeUnit.SECONDS)

I wish to use this, but don't know how to continue.

    val signin = GoogleSignIn.getClient(ctx, Global.getGSO())
    val task = signin.silentSignIn()

Solution

  • The Tasks class includes a static await method - in Java I'm doing this:

    GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(context, gso);
    
    Task<GoogleSignInAccount> task = googleSignInClient.silentSignIn();
    
    try {
        GoogleSignInAccount account = Tasks.await(task);
        ...
    } catch (ExecutionException e) {
        // task failed
    } catch (InterruptedException e) {
        // an interrupt occurred while waiting for the task to finish
    }