skip to Main Content

according to graphApi’s documentation :

Cursor-based Pagination

Cursor-based pagination is the most efficient method of paging and should always be used where possible. A
cursor refers to a random string of characters which marks a specific
item in a list of data. Unless this item is deleted, the cursor will
always point to the same part of the list, but is be invalidated if an
item is removed. Therefore, your app shouldn’t store any older cursors
or assume that they will still be valid.

When reading an edge that supports cursor pagination, you will see the
following JSON response:

    {
  "data": [
     ... Endpoint data is here
  ],
  "paging": {
    "cursors": {
      "after": "MTAxNTExOTQ1MjAwNzI5NDE=",
      "before": "NDMyNzQyODI3OTQw"
    },
    "previous": "https://graph.facebook.com/me/albums?limit=25&before=NDMyNzQyODI3OTQw"
    "next": "https://graph.facebook.com/me/albums?limit=25&after=MTAxNTExOTQ1MjAwNzI5NDE="
  }
}

but i have no idea what’s going on up here, anyone can point me on how we do Cursor-based Pagination here? i’ve done similar thing by passing the Max_id but in here its not the case

this is how i’m making my first call

    accessKey = "(appID)|(appSecret)"
    let connection = GraphRequestConnection()

    let request = GraphRequest.init(graphPath:"/myPageName/posts", parameters: [ "access_token" : accessKey, "fields" : "message,full_picture,created_time,story,type,permalink_url" , "limit": "10"], accessToken: nil, httpMethod: .GET, apiVersion: "2.8") 



        connection.add(request) { httpResponse, result in
        switch result {
        case .success(let response):

//FETCHED DATA HERE///

 case .failed(let error):
            print("Graph Request Failed: (error)")
        }
 }
    connection.start()

2

Answers


  1. (Posted on behalf of the OP).

    Passing offset in parameter did the job.

    Login or Signup to reply.
  2. For this lets first understand Cursor paging with an example :
    

    Let’s assume we want to paginate from the most recent user to the oldest user.When client request for the first time , suppose we select the first page through query:

    SELECT * FROM users
    WHERE team_id = %team_id
    ORDER BY id DESC
    LIMIT %limit
    

    Where limit is equal to limit plus one, to fetch one more result than the count specified by the client. The extra result isn’t returned in the result set, but we use the ID of the value as the next_cursor.

    The response from the server would be:

    {
       "users": [...],
       "next_cursor": "1234",  # the user id of the extra result
    }
    

    The client would then provide next_cursor as cursor in the second request.

    SELECT * FROM users
    WHERE team_id = %team_id
    AND id <= %cursor
    ORDER BY id DESC
    LIMIT %limit
    

    Coming back to Facebook Implementation : As the name suggests the next cursor will fetch you the next set of results and the previous cursor will fetch you the previous set of results.

    If you still have problem understanding Cursors and why they are used you can check out this article. The above example is quoted from the same.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search