How to use the Facebook Graph Api Cursor-based Pagination

后端 未结 5 2344
南旧
南旧 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:48

    Though it's true that you should use GraphResponse.getRequestForPagedResults(), you can't use executeAndWait() unless you run it in a different thread.

    You can make it even easier using executeAsync().

    To get the first set of results:

        new GraphRequest(AccessToken.getCurrentAccessToken(),
                "/" + facebookID + "/invitable_friends",
                null,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {
                        //your code
    
                        //save the last GraphResponse you received
                        lastGraphResponse = response;
                    }
                }
        ).executeAsync();
    

    Use that lastGraphResponse to get the next set of results:

        GraphRequest nextResultsRequests = lastGraphResponse.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
        if (nextResultsRequests != null) {
            nextResultsRequests.setCallback(new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                    //your code
    
                    //save the last GraphResponse you received
                    lastGraphResponse = response;
                }
            });
            nextResultsRequests.executeAsync();
        }
    

    You can merge all that in a single method!

提交回复
热议问题