How to use the Facebook Graph Api Cursor-based Pagination

后端 未结 5 2320
南旧
南旧 2021-02-13 10:15

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

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-13 10:40

    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.")
        }
    }
    

提交回复
热议问题