PendingResult

public abstract classPendingResult<R extendsResult>
Known direct subclasses
Batch

Handles a batch ofPendingResultitems.

OptionalPendingResult

EachOptionalPendingResultis aPendingResultwith additional support for non-blocking accessors.


Represents a pending result from calling an API method in Google Play services. The final result object from a PendingResultis of type R, which can be retrieved in one of two ways.

  • via blocking calls toawait,orawait,or
  • via a callback by passing in an object implementing interfaceResultCallbackto setResultCallback.

After the result has been retrieved using await or delivered to the result callback, it is an error to attempt to retrieve the result again. It is the responsibility of the caller or callback receiver to release any resources associated with the returned result. Some result types may implementReleasable,in which casereleaseshould be used to free the associated resources.

Parameters
<R extendsResult>

Result type

Summary

Public constructors

Public methods

abstract @NonNullR

Blocks until the task is completed.

abstract @NonNullR
await(long time, @NonNullTimeUnitunits)

Blocks until the task is completed or has timed out waiting for the result.

abstract void

Requests that the PendingResult be canceled.

abstract boolean

Indicates whether the pending result has been canceled either due to callingdisconnector callingcanceldirectly on the pending result or an enclosingBatch.

abstract void

Set the callback here if you want the result to be delivered via a callback when the result is ready.

abstract void

Set the callback here if you want the result to be delivered via a callback when the result is ready or has timed out waiting for the result.

@NonNullTransformedResult<S>
<S extendsResult>then(@NonNullResultTransform<Object,S> transform)

Transforms the result by making another API call.

Public constructors

PendingResult

publicPendingResult()

Public methods

await

public abstract @NonNullRawait()

Blocks until the task is completed. This is not allowed on the UI thread. The returned result object can have an additional failure mode ofINTERRUPTED.

await

public abstract @NonNullRawait(long time, @NonNullTimeUnitunits)

Blocks until the task is completed or has timed out waiting for the result. This is not allowed on the UI thread. The returned result object can have an additional failure mode of eitherINTERRUPTEDorTIMEOUT.

cancel

public abstract voidcancel()

Requests that the PendingResult be canceled. If the result is available, but not consumed it will be released. If the result is set after cancelation was requested it is immediately released.

onResultwill never be called,awaitwill return a failed result withCANCELED.

isCanceled

public abstract booleanisCanceled()

Indicates whether the pending result has been canceled either due to callingdisconnector callingcanceldirectly on the pending result or an enclosingBatch.

setResultCallback

public abstract voidsetResultCallback(@NonNullResultCallback<Object> callback)

Set the callback here if you want the result to be delivered via a callback when the result is ready.

setResultCallback

public abstract voidsetResultCallback(
@NonNullResultCallback<Object> callback,
long time,
@NonNullTimeUnitunits
)

Set the callback here if you want the result to be delivered via a callback when the result is ready or has timed out waiting for the result. The returned result object can have an additional failure mode ofTIMEOUT.

then

public @NonNullTransformedResult<S> <S extendsResult>then(@NonNullResultTransform<Object,S> transform)

Transforms the result by making another API call.

If the result is successful, thenonSuccesswill be called to make the additional API call that yields the transformed result. If the result is a failure, thenonFailurewill be called to (optionally) allow modification of failure status.

If the result implementsReleasable,thenreleasewill be called once the transform has been applied.

Multiple API calls can be chained together by making subsequent calls tothenand the final result can be received by an instance of specified viaandFinally.For example:

finalDriveFolderrootFolder=Drive.DriveApi.getRootFolder(mApiClient);
Drive.DriveApi.newDriveContents(mApiClient)
.then(newResultTransform<DriveContentsResult,DriveFileResult>(){
@Override
publicPendingResult<DriveFileResult>onSuccess(DriveContentsResultresult){
DriveContentsdriveContents=result.getDriveContents();

//WritecontenttoDriveContents
OutputStreamoutputStream=driveContents.getOutputStream();
PrintWriterwriter=newPrintWriter(outputStream);
writer.write("HelloWorld!");
writer.close();

MetadataChangeSetchangeSet=newMetadataChangeSet.Builder()
.setTitle("hello.txt")
.setMimeType("text/plain")
.build();

//Createafileonrootfolder
returnrootFolder.createFile(mApiClient,changeSet,driveContents);
}
})
.then(newResultTransform<DriveFileResult,MetadataBufferResult>(){
@Override
publicPendingResult<MetadataBufferResult>onSuccess(DriveFileResultresult){
//Fetchtheupdatedrootfoldercontents
returnrootFolder.listChildren(mApiClient)
}
})
.andFinally(newResultCallbacks<MetadataBufferResult>(){
@Override
publicvoidonSuccess(MetadataBufferResultresult){
//AllAPIcallsweresuccessful.
}

@Override
publicvoidonFailure(Statusstatus){
//AnAPIcallfailed.
}
});

AllResultTransforms will be run on a worker thread. These transforms therefore must not interact with UI elements, but they may perform brief background work (not requiring more than a few seconds). IfResultCallbacksare specified, these will be called on the thread specified bysetHandleror on the main thread by default.

Ifdisconnectis called before a series of transforms completes the transforms will continue to run in the background until the last one completes. In this case,ResultCallbackswill not be called. Note that this may cause memory leaks if background transformations are long-running. For long background tasks, consider using an.

Note: it is an error to use multipleGoogleApiClients for various API calls within subsequentResultTransforms. Behavior is undefined if calls don't use the same GoogleApiClient.