How to fetch more than 25 post messages

前端 未结 4 788

I\'m trying to get all post messages using restfb, my code is as follows

public Connection publicSearchMessages(Date fromDate, Date toDate) {
    Co         


        
4条回答
  •  孤独总比滥情好
    2021-02-14 18:26

    As I tested recently, You don't have to specify anything. Connection class implements Iterable in this way:

    • fetch 25 results
    • hasNext check if there is next item to process
    • if not, it will fetch next page of 25 results

    So basically all you need to do is this:

    Connection messages = publicFbClient.fetchConnection("search",
            Post.class,
            Parameter.with("q", "Watermelon"),
            Parameter.with("since", fromDate),
            Parameter.with("until", toDate),
            Parameter.with("type", "post"));
    
    for (List feedConnectionPage : messages) {
            for (Post post : myFeedConnectionPage) {
                     // do stuff with post
            }
    }
    

    If you want to have some kind of method that returns results I would be very careful, because you can be returning thousands of results and crawling through them may take some time (from seconds to minutes or even hours) and result object array is going to be really big. Better idea is to use some asynchronous call and check out results of method periodically.

    Though it seems that parameter "since" is ignored. Posts are fetched from newest to oldest and I think that it somehow leave out this parameter when doing paging.

    Hope I made it more clear for you :)

提交回复
热议问题