firebase::Future

#include <future.h>

Type-specific version ofFutureBase.

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 aFuture, and provide a "LastResult" function to retrieve the most recentFuture 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 aFuture from an asynchronous operation, it will eventually complete. Once it is complete, you can check for errors (a nonzeroerror() means an error occurred) and get the result data if no error occurred by callingresult().

There are two ways to find out that aFuture has completed. You can poll itsstatus(), or set anOnCompletion() 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([](constFuture<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 thisFuture'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(constFuture< 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)(constFuture<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

voidOnCompletion(TypedCompletionCallbackcallback,void*user_data)const

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

If you call anyOnCompletion() 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.

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

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

OnCompletion

voidOnCompletion(std::function<void(constFuture<ResultType>&)>callback)const

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

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

Note: This method is not available when using STLPort on Android, asstd::function is not supported on STLPort.Note: This is the same callback asFutureBase::OnCompletion(), so you can't expect to set both and have both run; again, only the most recently registered one will run.

Details
Parameters
callback
Function or lambda to call.

result

constResultType*result()const

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

Allows the API to provide a type-specific interface.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-01-23 UTC.