ResponseDataSource

class ResponseDataSource<T> : DataSource<T>

Author

skydoves (Jaewoong Eum)

ResponseDataSource is an implementation of the DataSource interface.

Asynchronously send requests and A response data holder from the REST API call. Support observer for the every request responses, concat another DataSource, Retry fetching data when the request gets failure.

Constructors

Link copied to clipboard
fun ResponseDataSource()

Properties

Link copied to clipboard
var call: Call<T>? = null
Link copied to clipboard
var callback: Callback<T>? = null
Link copied to clipboard
var concatStrategy: DataSource.ConcatStrategy
Link copied to clipboard
var data: Any

Functions

Link copied to clipboard
fun asLiveData(): LiveData<T>

if the response is successful, it returns a LiveData which contains response data. if the response is failure or exception, it returns an empty LiveData. this live data can be observable from the network requests.

Link copied to clipboard
inline fun combine(call: Call<T>, crossinline onResult: (response: ApiResponse<T>) -> Unit): ResponseDataSource<T>
open override fun combine(call: Call<T>, callback: Callback<T>?): ResponseDataSource<T>

combine a call and callback instances for caching data.

Link copied to clipboard
open override fun <R> concat(dataSource: DataSource<R>): DataSource<R>

concat an another DataSource and request API call sequentially if the API call getting successful.

Link copied to clipboard
fun dataRetainPolicy(dataRetainPolicy: DataRetainPolicy): ResponseDataSource<T>

sets DataRetainPolicy for limiting retaining data.

Link copied to clipboard
open override fun invalidate()

invalidate a cached data and re-fetching the API request.

Link copied to clipboard
open override fun joinDisposable(disposable: CompositeDisposable): ResponseDataSource<T>

joins onto CompositeDisposable as a disposable. must be called before request.

Link copied to clipboard
open override fun observeResponse(observer: ResponseObserver<T>): ResponseDataSource<T>
inline fun observeResponse(crossinline action: (ApiResponse<T>) -> Unit): ResponseDataSource<T>

observes a ApiResponse value from the API call request.

Link copied to clipboard
fun postValue(value: ApiResponse<T>)

sets value on the worker thread and post the value to the main thread.

Link copied to clipboard
open override fun request(): ResponseDataSource<T>

request API network call asynchronously. if the request is successful, this data source will hold the success response model. in the next request after success, returns the cached API response. if you need to fetch a new response data or refresh, use invalidate().

inline fun request(crossinline action: ApiResponse<T>.() -> Unit): ResponseDataSource<T>

extension method for requesting and observing response at once.

Link copied to clipboard
open override fun retry(retryCount: Int, interval: Long): ResponseDataSource<T>

Retry requesting API call when the request gets failure.

Link copied to clipboard
inline fun suspendCombine(    call: Call<T>,     context: CoroutineContext = EmptyCoroutineContext,     crossinline onResult: suspend (response: ApiResponse<T>) -> Unit): ResponseDataSource<T>

combine a call and callback instances for caching data on a CoroutineContext.

inline fun suspendCombine(    call: Call<T>,     coroutineScope: CoroutineScope,     crossinline onResult: suspend (response: ApiResponse<T>) -> Unit): ResponseDataSource<T>

combine a call and callback instances for caching data on a CoroutineScope.

Link copied to clipboard
inline fun suspendRequest(context: CoroutineContext = EmptyCoroutineContext, crossinline action: suspend ApiResponse<T>.() -> Unit): ResponseDataSource<T>

extension method for requesting and observing response at once with a CoroutineContext.

inline fun suspendRequest(coroutineScope: CoroutineScope, crossinline action: suspend ApiResponse<T>.() -> Unit): ResponseDataSource<T>

extension method for requesting and observing response at once on a CoroutineScope.