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([](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);
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-01-23 UTC."],[],[],null,["firebase::Future\n\n\n`#include \u003cfuture.h\u003e`\n\nType-specific version of [FutureBase](/docs/reference/cpp/class/firebase/future-base#classfirebase_1_1_future_base).\n\nSummary\n\nThe 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](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future), and provide a \"LastResult\" function to retrieve the most recent [Future](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future) result.\n\n\n```c++\n// You can retrieve the Future from the function call directly, like this:\nFuture\u003c SampleResultType \u003e future = firebase::SampleAsyncOperation();\n\n// Or you can retrieve it later, like this:\nfirebase::SampleAsyncOperation();\n// [...]\nFuture\u003c SampleResultType \u003e future =\n firebase::SampleAsyncOperationLastResult();\n```\n\n\u003cbr /\u003e\n\nWhen you have a [Future](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future) from an asynchronous operation, it will eventually complete. Once it is complete, you can check for errors (a nonzero [error()](/docs/reference/cpp/class/firebase/future-base#classfirebase_1_1_future_base_1ab7a9ffdf8f16b4473291219d244db26c) means an error occurred) and get the result data if no error occurred by calling [result()](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future_1a919675755a271575134464a01788b41d).\n\nThere are two ways to find out that a [Future](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future) has completed. You can poll its [status()](/docs/reference/cpp/class/firebase/future-base#classfirebase_1_1_future_base_1a7b1a3fdc54483440d1ec8d0dc4a64883), or set an [OnCompletion()](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future_1a334de3fc89aefc4704c89f3fb7ba1c08) callback:\n\n\n```c++\n// Check whether the status is kFutureStatusComplete.\nif (future.status() == firebase::kFutureStatusComplete) {\n if (future.error() == 0) {\n DoSomethingWithResultData(future.result());\n }\n else {\n LogMessage(\"Error %d: %s\", future.error(), future.error_message());\n }\n}\n\n// Or, set an OnCompletion callback, which accepts a C++11 lambda or\n// function pointer. You can pass your own user data to the callback. In\n// most cases, the callback will be running in a different thread, so take\n// care to make sure your code is thread-safe.\nfuture.OnCompletion([](const Future\u003c SampleResultType \u003e& completed_future,\n void* user_data) {\n // We are probably in a different thread right now.\n if (completed_future.error() == 0) {\n DoSomethingWithResultData(completed_future.result());\n }\n else {\n LogMessage(\"Error %d: %s\",\n completed_future.error(),\n completed_future.error_message());\n }\n}, user_data);\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nInheritanceInherits from: [firebase::FutureBase](/docs/reference/cpp/class/firebase/future-base)\n\nPublic types \n\nTypedCompletionCallback \n\n```c++\nvoid(* TypedCompletionCallback)(const Future\u003c ResultType \u003e &result_data, void *user_data)\n``` \nFunction pointer for a completion callback.\n\nWhen we call this, we will send the completed future, along with the user data that you specified when you set up the callback.\n\nPublic functions \n\nFuture \n\n```c++\n Future()\n``` \nConstruct a future. \n\nOnCompletion \n\n```c++\nvoid OnCompletion(\n TypedCompletionCallback callback,\n void *user_data\n) const \n``` \nRegister a single callback that will be called at most once, when the future is completed.\n\nIf you call any [OnCompletion()](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future_1a334de3fc89aefc4704c89f3fb7ba1c08) method more than once on the same future, only the most recent callback you registered will be called.\n\nWhen your callback is called, the user_data that you supplied here will be passed back as the second parameter.\n\n\n| **Note:** This is the same callback as [FutureBase::OnCompletion()](/docs/reference/cpp/class/firebase/future-base#classfirebase_1_1_future_base_1a963b7b47edaecabcd58303632d9e45ca), so you can't expect to set both and have both run; again, only the most recently registered one will run.\n\n\u003cbr /\u003e\n\nOnCompletion \n\n```c++\nvoid OnCompletion(\n std::function\u003c void(const Future\u003c ResultType \u003e &)\u003e callback\n) const \n``` \nRegister a single callback that will be called at most once, when the future is completed.\n\nIf you call any [OnCompletion()](/docs/reference/cpp/class/firebase/future#classfirebase_1_1_future_1a334de3fc89aefc4704c89f3fb7ba1c08) method more than once on the same future, only the most recent callback you registered will be called.\n\n\n| **Note:** This method is not available when using STLPort on Android, as `std::function` is not supported on STLPort.\n| **Note:** This is the same callback as [FutureBase::OnCompletion()](/docs/reference/cpp/class/firebase/future-base#classfirebase_1_1_future_base_1a963b7b47edaecabcd58303632d9e45ca), so you can't expect to set both and have both run; again, only the most recently registered one will run.\n\n\u003cbr /\u003e\n\nresult \n\n```c++\nconst ResultType * result() const \n``` \nResult of the asynchronous call, or nullptr if the result is still pending.\n\nAllows the API to provide a type-specific interface."]]