Skip to content

Instantly share code, notes, and snippets.

@Gnzlt
Last active July 16, 2019 14:41
Show Gist options
  • Select an option

  • Save Gnzlt/2aef24f3aea1ceed87ded5cdbbc46553 to your computer and use it in GitHub Desktop.

Select an option

Save Gnzlt/2aef24f3aea1ceed87ded5cdbbc46553 to your computer and use it in GitHub Desktop.
Android Arch Component Paging Retryable DataSource
package com.example
import androidx.paging.PositionalDataSource
abstract class RetryableDataSource : PositionalDataSource<String>() {
private var retryAction: (() -> Any)? = null
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<String>) {
try {
val result = listOf("Api result data")
callback.onResult(result)
} catch (exception: Exception) {
retryAction = { loadRange(params, callback) }
callback.onResult(emptyList())
}
}
fun retry() {
if (retryAction != null) {
retryAction!!.invoke()
retryAction = null
} else {
invalidate()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment