firebase::Future

#include <future.h>

Type-specific version of FutureBase.

Summary

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:
firebase::SampleAsyncOperation();
// [...]
Future< SampleResultType > future =
    firebase::SampleAsyncOperationLastResult();

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) {
    DoSomethingWithResultData(future.result());
  }
  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) {
    DoSomethingWithResultData(completed_future.result());
  }
  else {
    LogMessage("Error %d: %s",
               completed_future.error(),
               completed_future.error_message());
  }
}, user_data);

Details
Template Parameters
ResultType
The type of this Future's result.

Inheritance

Inherits from: firebase::FutureBase

Constructors and Destructors

Future()
Construct a future.

Public types

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

Public functions

OnCompletion(TypedCompletionCallback callback, void *user_data) const
void
Register a single callback that will be called at most once, when the future is completed.
OnCompletion(std::function< void(const Future< ResultType > &)> callback) const
void
Register a single 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

TypedCompletionCallback

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

Future

 Future()

Construct a future.

OnCompletion

void OnCompletion(
  TypedCompletionCallback callback,
  void *user_data
) const 

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

If you call any OnCompletion() method more than once on the same future, 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.

Details
Parameters
callback
Function pointer to your callback.
user_data
Optional user data. We will pass this back to your callback.

OnCompletion

void OnCompletion(
  std::function< void(const Future< ResultType > &)> callback
) const 

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

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

Details
Parameters
callback
Function or lambda to call.

result

const ResultType * result() const 

Result of the asynchronous call, or nullptr if the result is still pending.

Allows the API to provide a type-specific interface.