Futures represent the eventual completion of an asynchronous operation. A future has one of threeFutureStates, which can be obtained withgetState():
FutureState.PENDING- The operation is still pending. The result of the operation
isn't available yet and any associated callback hasn't yet been invoked.
FutureState.DONE- The operation is complete, and a result is available.
The operation's result can be reported via acallback. When providing a callback,
ARCore will invoke the given function when the operation is complete, unless the future has been
cancelled usingcancel(). This callback will be invoked on themain
thread.
Cancelling a Future
You can try to cancel aFutureby callingcancel(). Due to
multi-threading, it is possible that the cancel operation is not successful. The return value
indicates if the cancellation was successful.
If the cancellation is successful, then anyassociated callbackwill never be called.
Tries to cancel the execution of this operation. If the operation was cancelled by this
invocation, this method returnstrueand the associated callback (if any) will never be
invoked.
[[["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-10-31 UTC."],[[["\u003cp\u003e\u003ccode\u003eFuture\u003c/code\u003e in ARCore represents the eventual result of an asynchronous operation, such as resolving a cloud anchor or checking VPS availability.\u003c/p\u003e\n"],["\u003cp\u003eYou can obtain the result of a \u003ccode\u003eFuture\u003c/code\u003e either by polling its state using \u003ccode\u003egetState()\u003c/code\u003e or by providing a callback function that will be invoked upon completion.\u003c/p\u003e\n"],["\u003cp\u003eA \u003ccode\u003eFuture\u003c/code\u003e can be cancelled using \u003ccode\u003ecancel()\u003c/code\u003e, preventing the associated callback from being invoked, but cancellation is not always guaranteed due to multi-threading.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eFuture\u003c/code\u003e has three states: \u003ccode\u003ePENDING\u003c/code\u003e, \u003ccode\u003eDONE\u003c/code\u003e, and \u003ccode\u003eCANCELLED\u003c/code\u003e, reflecting the status of the asynchronous operation.\u003c/p\u003e\n"],["\u003cp\u003eSeveral ARCore operations, including hosting and resolving cloud anchors, resolving anchors on terrain or rooftops, and checking VPS availability, utilize \u003ccode\u003eFuture\u003c/code\u003e objects to handle their asynchronous nature.\u003c/p\u003e\n"]]],[],null,["# Future\n\n`\npublic interface\n`**Future**`\n` \n\n|---|---|---|\n| Known Indirect Subclasses [HostCloudAnchorFuture](/ar/reference/java/com/google/ar/core/HostCloudAnchorFuture), [ResolveAnchorOnRooftopFuture](/ar/reference/java/com/google/ar/core/ResolveAnchorOnRooftopFuture), [ResolveAnchorOnTerrainFuture](/ar/reference/java/com/google/ar/core/ResolveAnchorOnTerrainFuture), [ResolveCloudAnchorFuture](/ar/reference/java/com/google/ar/core/ResolveCloudAnchorFuture), [VpsAvailabilityFuture](/ar/reference/java/com/google/ar/core/VpsAvailabilityFuture) |----------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [HostCloudAnchorFuture](/ar/reference/java/com/google/ar/core/HostCloudAnchorFuture) | Handle to an async operation launched by [Session.hostCloudAnchorAsync(Anchor, int, BiConsumer)](/ar/reference/java/com/google/ar/core/Session#hostCloudAnchorAsync(com.google.ar.core.Anchor,%20int,%20java.util.function.BiConsumer\u003cjava.lang.String,%20com.google.ar.core.Anchor.CloudAnchorState\u003e)). | | [ResolveAnchorOnRooftopFuture](/ar/reference/java/com/google/ar/core/ResolveAnchorOnRooftopFuture) | Handle to an async operation launched by [Earth.resolveAnchorOnRooftopAsync(double, double, double, float, float, float, float, BiConsumer)](/ar/reference/java/com/google/ar/core/Earth#resolveAnchorOnRooftopAsync(double,%20double,%20double,%20float,%20float,%20float,%20float,%20java.util.function.BiConsumer\u003ccom.google.ar.core.Anchor,%20com.google.ar.core.Anchor.RooftopAnchorState\u003e)). | | [ResolveAnchorOnTerrainFuture](/ar/reference/java/com/google/ar/core/ResolveAnchorOnTerrainFuture) | Handle to an async operation launched by [Earth.resolveAnchorOnTerrainAsync(double, double, double, float, float, float, float, BiConsumer)](/ar/reference/java/com/google/ar/core/Earth#resolveAnchorOnTerrainAsync(double,%20double,%20double,%20float,%20float,%20float,%20float,%20java.util.function.BiConsumer\u003ccom.google.ar.core.Anchor,%20com.google.ar.core.Anchor.TerrainAnchorState\u003e)). | | [ResolveCloudAnchorFuture](/ar/reference/java/com/google/ar/core/ResolveCloudAnchorFuture) | Handle to an async operation launched by [Session.resolveCloudAnchorAsync(String, BiConsumer)](/ar/reference/java/com/google/ar/core/Session#resolveCloudAnchorAsync(java.lang.String,%20java.util.function.BiConsumer\u003ccom.google.ar.core.Anchor,%20com.google.ar.core.Anchor.CloudAnchorState\u003e)). | | [VpsAvailabilityFuture](/ar/reference/java/com/google/ar/core/VpsAvailabilityFuture) | Handle to an async operation launched by [Session.checkVpsAvailabilityAsync(double, double, Consumer)](/ar/reference/java/com/google/ar/core/Session#checkVpsAvailabilityAsync(double,%20double,%20java.util.function.Consumer\u003ccom.google.ar.core.VpsAvailability\u003e)). | |||\n\nFutures represent the eventual completion of an asynchronous operation. A future has one of three\n[FutureState](/ar/reference/java/com/google/ar/core/FutureState)s, which can be obtained with [getState()](/ar/reference/java/com/google/ar/core/Future#getState()):\n\n- [FutureState.PENDING](/ar/reference/java/com/google/ar/core/FutureState#PENDING) - The operation is still pending. The result of the operation isn't available yet and any associated callback hasn't yet been invoked.\n- [FutureState.DONE](/ar/reference/java/com/google/ar/core/FutureState#DONE) - The operation is complete, and a result is available.\n- [FutureState.CANCELLED](/ar/reference/java/com/google/ar/core/FutureState#CANCELLED) - The operation has been cancelled.\n\nA [Future](/ar/reference/java/com/google/ar/core/Future) starts in the [FutureState.PENDING](/ar/reference/java/com/google/ar/core/FutureState#PENDING) state and transitions to [FutureState.DONE](/ar/reference/java/com/google/ar/core/FutureState#DONE) upon completion. If the future is cancelled using [cancel()](/ar/reference/java/com/google/ar/core/Future#cancel()), then\nits state may become [FutureState.CANCELLED](/ar/reference/java/com/google/ar/core/FutureState#CANCELLED) (see [cancelling\na future](#future_cancellation) for caveats).\n\n### Obtaining results from a Future\n\nThere are two ways of obtaining results from a [Future](/ar/reference/java/com/google/ar/core/Future):\n\n#### Polling a Future\n\nWhen the [Future](/ar/reference/java/com/google/ar/core/Future) is created, its [FutureState](/ar/reference/java/com/google/ar/core/FutureState) is set to [FutureState.PENDING](/ar/reference/java/com/google/ar/core/FutureState#PENDING). You may poll the future using [getState()](/ar/reference/java/com/google/ar/core/Future#getState()) to query the state of\nthe asynchronous operation. When its state is [FutureState.DONE](/ar/reference/java/com/google/ar/core/FutureState#DONE), you can obtain the\noperation's result.\n\n#### Using a callback to obtain Future results\n\nThe operation's result can be reported via a `callback`. When providing a callback,\nARCore will invoke the given function when the operation is complete, unless the future has been\ncancelled using [cancel()](/ar/reference/java/com/google/ar/core/Future#cancel()). This callback will be invoked on the [main\nthread](https://developer.android.com/guide/components/processes-and-threads#Threads).\n\n#### Cancelling a Future\n\nYou can try to cancel a [Future](/ar/reference/java/com/google/ar/core/Future) by calling [cancel()](/ar/reference/java/com/google/ar/core/Future#cancel()). Due to\nmulti-threading, it is possible that the cancel operation is not successful. The return value\nindicates if the cancellation was successful.\n\nIf the cancellation is successful, then any [associated callback](#future_callback)\nwill never be called. \n\nPublic Methods\n--------------\n\n|---------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|\n| ` abstract boolean ` | [cancel](/ar/reference/java/com/google/ar/core/Future#cancel())`()` Tries to cancel the execution of this operation. |\n| ` abstract `[FutureState](/ar/reference/java/com/google/ar/core/FutureState)` ` | [getState](/ar/reference/java/com/google/ar/core/Future#getState())`()` Get the current state of the future. |\n\nPublic Methods\n--------------\n\n#### public\nabstract\nboolean\ncancel\n()\n\n### cancel\n\n```java\npublic abstract boolean cancel()\n``` \nTries to cancel the execution of this operation. If the operation was cancelled by this\ninvocation, this method returns `true` and the associated callback (if any) will never be\ninvoked.\n\n#### public\nabstract\n[FutureState](/ar/reference/java/com/google/ar/core/FutureState)\ngetState\n()\n\n### getState\n\n```java\npublic abstract FutureState getState()\n``` \nGet the current state of the future."]]