API задач

API Task — это стандартный способ обработки асинхронных операций в сервисах Google Play. Он предоставляет мощный и гибкий способ управления асинхронными вызовами, заменяя устаревший шаблон PendingResult . С помощью Task вы можете объединять несколько вызовов, обрабатывать сложные потоки и писать понятные обработчики успешного выполнения и сбоев.

Handle task results

Many APIs in Google Play services and Firebase return a Task object to represent asynchronous operations. For example, FirebaseAuth.signInAnonymously() returns a Task<AuthResult> which represents the result of the sign-in operation. The Task<AuthResult> indicates that when the task completes successfully, it will return an AuthResult object.

Вы можете обрабатывать результат Task , добавляя обработчики событий, которые реагируют на успешное завершение, сбой или и то, и другое:

Task<AuthResult> task = FirebaseAuth.getInstance().signInAnonymously();

Для обработки успешного завершения задачи добавьте OnSuccessListener :

task.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
    @Override
    public void onSuccess(AuthResult authResult) {
        // Task completed successfully
        // ...
    }
});

To handle a failed task, attach an OnFailureListener :

task.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        // Task failed with an exception
        // ...
    }
});

Чтобы обрабатывать как успешные, так и неудачные попытки в одном и том же обработчике событий, добавьте обработчик OnCompleteListener :

task.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            // Task completed successfully
            AuthResult result = task.getResult();
        } else {
            // Task failed with an exception
            Exception exception = task.getException();
        }
    }
});

Управление потоками

By default, listeners attached to a Task are run on the application main (UI) thread. This means that you should avoid doing long-running operations in listeners. If you need to perform a long-running operation, you can specify an Executor that is used to schedule listeners on a background thread.

// Create a new ThreadPoolExecutor with 2 threads for each processor on the
// device and a 60 second keep-alive time.
int numCores = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executor = new ThreadPoolExecutor(numCores * 2, numCores *2,
        60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

task.addOnCompleteListener(executor, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        // ...
    }
});

Use activity-scoped listeners

When you need to handle task results within an Activity , it's important to manage the listeners' lifecycle to prevent them from being called when the Activity is no longer visible. To do this, you can use activity-scoped listeners. These listeners are automatically removed when the onStop method of your Activity is called, so that they won't be executed after the Activity is stopped.

Activity activity = MainActivity.this;
task.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        // ...
    }
});

Цепочка задач

Если вы используете набор API, возвращающих объекты Task , в сложной функции, вы можете объединять их в цепочку с помощью продолжений. Это помогает избежать глубоко вложенных коллбэков и объединяет обработку ошибок для нескольких связанных задач.

For example, consider a scenario where you have a method doSomething that returns a Task<String> , but it requires an AuthResult as a parameter. You can obtain this AuthResult asynchronously from another Task :

public Task<String> doSomething(AuthResult authResult) {
    // ...
}

Using the Task.continueWithTask method, you can chain these two tasks:

Task<AuthResult> signInTask = FirebaseAuth.getInstance().signInAnonymously();

signInTask.continueWithTask(new Continuation<AuthResult, Task<String>>() {
    @Override
    public Task<String> then(@NonNull Task<AuthResult> task) throws Exception {
        // Take the result from the first task and start the second one
        AuthResult result = task.getResult();
        return doSomething(result);
    }
}).addOnSuccessListener(new OnSuccessListener<String>() {
    @Override
    public void onSuccess(String s) {
        // Chain of tasks completed successfully, got result from last task.
        // ...
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        // One of the tasks in the chain failed with an exception.
        // ...
    }
});

Заблокировать задачу

Если ваша программа уже выполняется в фоновом потоке, вы можете заблокировать текущий поток и дождаться завершения задачи, вместо использования функции обратного вызова:

try {
    // Block on a task and get the result synchronously. This is generally done
    // when executing a task inside a separately managed background thread. Doing this
    // on the main (UI) thread can cause your application to become unresponsive.
    AuthResult authResult = Tasks.await(task);
} catch (ExecutionException e) {
    // The Task failed, this is the same exception you'd get in a non-blocking
    // failure handler.
    // ...
} catch (InterruptedException e) {
    // An interrupt occurred while waiting for the task to complete.
    // ...
}

Также можно указать тайм-аут при блокировке задачи, чтобы предотвратить бесконечное зависание приложения, если выполнение задачи занимает слишком много времени:

try {
    // Block on the task for a maximum of 500 milliseconds, otherwise time out.
    AuthResult authResult = Tasks.await(task, 500, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
    // ...
} catch (InterruptedException e) {
    // ...
} catch (TimeoutException e) {
    // Task timed out before it could complete.
    // ...
}

Взаимодействие

Task is designed to work well with other common Android asynchronous programming patterns. It can be converted to and from other primitives like ListenableFuture and Kotlin coroutines, which are recommended by AndroidX , allowing you to use the approach that best fits your needs.

Here's an example using a Task :

// ...
simpleTask.addOnCompleteListener(this) {
  completedTask -> textView.text = completedTask.result
}

Корутины Kotlin

Чтобы использовать корутины Kotlin с Task , добавьте следующую зависимость в свой проект, а затем используйте приведенный фрагмент кода для преобразования из Task .

Gradle (файл build.gradle на уровне модуля, обычно app/build.gradle )
// Source: https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-play-services
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.3'
Фрагмент
import kotlinx.coroutines.tasks.await
// ...
  textView.text = simpleTask.await()
}

Guava ListenableFuture

To use Guava ListenableFuture with Task , add the following dependency to your project and then use the code snippet to convert from a Task .

Gradle (файл build.gradle на уровне модуля, обычно app/build.gradle )
implementation "androidx.concurrent:concurrent-futures:1.2.0"
Фрагмент
import com.google.common.util.concurrent.ListenableFuture
// ...
/** Convert Task to ListenableFuture. */
fun <T> taskToListenableFuture(task: Task<T>): ListenableFuture<T> {
  return CallbackToFutureAdapter.getFuture { completer ->
    task.addOnCompleteListener { completedTask ->
      if (completedTask.isCanceled) {
        completer.setCancelled()
      } else if (completedTask.isSuccessful) {
        completer.set(completedTask.result)
      } else {
        val e = completedTask.exception
        if (e != null) {
          completer.setException(e)
        } else {
          throw IllegalStateException()
        }
      }
    }
  }
}
// ...
this.listenableFuture = taskToListenableFuture(simpleTask)
this.listenableFuture?.addListener(
  Runnable {
    textView.text = listenableFuture?.get()
  },
  ContextCompat.getMainExecutor(this)
)

RxJava2 Observable

Добавьте в свой проект следующую зависимость, а также соответствующую асинхронную библиотеку по вашему выбору, и затем используйте приведенный фрагмент кода для преобразования из Task .

Gradle (файл build.gradle на уровне модуля, обычно app/build.gradle )
// Source: https://github.com/ashdavies/rx-tasks
implementation 'io.ashdavies.rx.rxtasks:rx-tasks:2.2.0'
Фрагмент
import io.ashdavies.rx.rxtasks.toSingle
import java.util.concurrent.TimeUnit
// ...
simpleTask.toSingle(this).subscribe { result -> textView.text = result }

Следующие шаги