कंसोल पर जाएं

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