Last active
July 16, 2019 14:41
-
-
Save Gnzlt/2aef24f3aea1ceed87ded5cdbbc46553 to your computer and use it in GitHub Desktop.
Android Arch Component Paging Retryable DataSource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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