I didn\'t find any help on this topic. The Docs say
Cursor-based pagination is the most efficient method of paging and should always be used where possibl
What I did in my project is basically doing the first call in onSearchClicked()
and getting result in onCompleted()
callback in ViewModel. If nextRequest
is null, I return the list of results back to my fragment, if not, I do the next call, until the end is reached (nextRequest == null
). Then, the accumulated list is returned to the fragment.
class SearchViewModel() : GraphRequest.Callback {
private var allPlaces = arrayListOf()
fun onSearchClicked(location: Location) {
val request = GraphRequest.newGraphPathRequest(accessToken, "/search", this)
request.parameters = parameters
request.executeAsync()
}
override fun onCompleted(response: GraphResponse?) {
if (response != null) {
allPlaces.addAll(response.data)
val nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT)
if (nextRequest == null) {
uiEvent.value = ShowPlaces(allPlaces)
} else {
nextRequest.callback = this
nextRequest.executeAsync()
}
} else {
Log.e(TAG, "Search response is null.")
}
}