कंसोल पर जाएं


#include <future.h>

Type-specific version of FutureBase.


The Firebase C++ SDK uses this class to return results from asynchronous operations. All Firebase C++ functions and method calls that operate asynchronously return a Future, and provide a "LastResult" function to retrieve the most recent Future result.

// You can retrieve the Future from the function call directly, like this:
Future< SampleResultType > future = firebase::SampleAsyncOperation();

// Or you can retrieve it later, like this:
// [...]
Future< SampleResultType > future =

When you have a Future from an asynchronous operation, it will eventually complete. Once it is complete, you can check for errors (a nonzero error() means an error occurred) and get the result data if no error occurred by calling result().

There are two ways to find out that a Future has completed. You can poll its status(), or set an OnCompletion() callback:

// Check whether the status is kFutureStatusComplete.
if (future.status() == firebase::kFutureStatusComplete) {
  if (future.error() == 0) {
  else {
    LogMessage("Error %d: %s", future.error(), future.error_message());

// Or, set an OnCompletion callback, which accepts a C++11 lambda or
// function pointer. You can pass your own user data to the callback. In
// most cases, the callback will be running in a different thread, so take
// care to make sure your code is thread-safe.
future.OnCompletion([](const Future< SampleResultType >& completed_future,
                       void* user_data) {
  // We are probably in a different thread right now.
  if (completed_future.error() == 0) {
  else {
    LogMessage("Error %d: %s",
}, user_data);

Template Parameters
The type of this Future's result.


Inherits from: firebase::FutureBase

Constructors and Destructors

Construct a future.

Public types

TypedCompletionCallback)(const Future< ResultType > &result_data, void *user_data) typedef
Function pointer for a completion callback.

Public functions

OnCompletion(TypedCompletionCallback callback, void *user_data) const
Register a callback that will be called at most once, when the future is completed.
OnCompletion(std::function< void(const Future< ResultType > &)> callback) const
Register a callback that will be called at most once, when the future is completed.
result() const
const ResultType *
Result of the asynchronous call, or nullptr if the result is still pending.

Public types


void(* TypedCompletionCallback)(const Future< ResultType > &result_data, void *user_data)

Function pointer for a completion callback.

When we call this, we will send the completed future, along with the user data that you specified when you set up the callback.

Public functions



Construct a future.


void OnCompletion(
  TypedCompletionCallback callback,
  void *user_data
) const 

Register a callback that will be called at most once, when the future is completed.

If you call any OnCompletion() method more than once, only the most recent callback you registered will be called.

When your callback is called, the user_data that you supplied here will be passed back as the second parameter.

Note:This is the same callback as FutureBase::OnCompletion(), so you can't expect to set both and have both run; again, only the most recently registered one will run.