public interface

DataSource

com.facebook.datasource.DataSource<T>
Known Indirect Subclasses

Class Overview

An alternative to Java Futures for the image pipeline.

Unlike Futures, DataSource can issue a series of results, rather than just one. A prime example is decoding progressive images, which have a series of intermediate results before the final one.

DataSources MUST be closed (close() is called on the DataSource) else resources may leak.

Summary

Public Methods
abstract boolean close()
Cancels the ongoing request and releases all associated resources.
abstract Map<String, Object> getExtras()
abstract Throwable getFailureCause()
abstract float getProgress()
abstract T getResult()
The most recent result of the asynchronous computation.
abstract boolean hasFailed()
abstract boolean hasMultipleResults()
abstract boolean hasResult()
abstract boolean isClosed()
abstract boolean isFinished()
abstract void subscribe(DataSubscriber<T> dataSubscriber, Executor executor)
Subscribe for notifications whenever the state of the DataSource changes.

Public Methods

public abstract boolean close ()

Cancels the ongoing request and releases all associated resources.

Subsequent calls to getResult() will return null.

Returns
  • true if the data source is closed for the first time

public abstract Map<String, Object> getExtras ()

Returns
  • an object with extra data for this datasource

public abstract Throwable getFailureCause ()

Returns
  • failure cause if the source has failed, else null

public abstract float getProgress ()

Returns
  • progress in range [0, 1]

public abstract T getResult ()

The most recent result of the asynchronous computation.

The caller gains ownership of the object and is responsible for releasing it. Note that subsequent calls to getResult might give different results. Later results should be considered to be of higher quality.

This method will return null in the following cases:

  • when the DataSource does not have a result (hasResult returns false).
  • when the last result produced was null.

Returns
  • current best result

public abstract boolean hasFailed ()

Returns
  • true if request finished due to error

public abstract boolean hasMultipleResults ()

Returns
  • true if the data source has multiple results (e.g. multiple images). This can be used for example by a RetainingDataSource to correctly display images.

public abstract boolean hasResult ()

Returns
  • true if any result (possibly of lower quality) is available right now, false otherwise

public abstract boolean isClosed ()

Returns
  • true if the data source is closed, false otherwise

public abstract boolean isFinished ()

Returns
  • true if request is finished, false otherwise

public abstract void subscribe (DataSubscriber<T> dataSubscriber, Executor executor)

Subscribe for notifications whenever the state of the DataSource changes.

All changes will be observed on the provided executor.