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.ErrorMessage());
}
}
// 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.ErrorMessage());
}
}, user_data);
| Details | |||
|---|---|---|---|
| Template Parameters |
|
Inheritance
Inherits from: firebase::FutureBase
Constructors and Destructors |
|
|---|---|
Future()
Construct a future.
|
Public types |
|
|---|---|
TypedCompletionCallback)(const Future< ResultType > &result_data, void *user_data)
|
typedefvoid(*
Function pointer for a completion callback. |
Public functions |
|
|---|---|
OnCompletion(TypedCompletionCallback callback, void *user_data) const
|
void
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
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 callback that will be called at most once, when the future is completed.
If you call OnCompletion() itself 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.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
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.

