0

Most of the Google Play Services remote function calls return a Task that will be completed some time in the future. I have multiple tasks that I want to wait but no longer than a certain time out.

There is Tasks.await() that will wait on one task with a time out, this will block the calling thread. Is there something that is available or can be created in the line of:

waitWithTimeout(2, TimeUnit.SECONDS, task1, task2) { results: List<Task> ->
    // we will be here if both tasks are completed 
    // or when 2 seconds is up, whichever is earlier
}
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228

1 Answers1

0

https://developers.google.com/android/reference/com/google/android/gms/tasks/Tasks

public static Task>> whenAllComplete (Task... tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete. This Task would always succeed even if any of the provided Tasks fail or canceled. Does not accept nulls.

Throws

NullPointerException

if any of the provided Tasks are null

public static Task>> whenAllComplete (Collection> tasks)

Returns a Task with a list of Tasks that completes successfully when all of the specified Tasks complete. This Task would always succeed even if any of the provided Tasks fail or canceled. Does not accept nulls.

The below shows this implemented in an example:

Does Tasks.whenAllSuccess guarantee the order in which I pass tasks to it?

  1. Create a List object to track which individual tasks completed

  2. Implement the individual tasks as cancellable tasks with an on complete listener which adds completed ones to the list created in step https://developers.google.com/android/reference/com/google/firebase/storage/CancellableTask.

  3. Then, start the timer for x seconds.

  4. Then, implement the whenallcomplete listener.

  5. If the whenallcomplete listener completes before the timer, cancel the timer and proceed.

  6. Else, if the timer runs out first, cancel the remaining tasks if desired and proceed using the completed list object in step 1.