I'm building an app using retrofit. Everything's working swimmingly, but I'm worried about the size of my API requests and would like to split them up using pagination.
What would be the best strategy to page through the API automatically with Retrofit, so that all available data is downloaded by default?
First, the pagination would need to be supported by the backend service that you're using. Second if you're looking to get an example of how this can be implemented from the client side using retrofit I would recommend you take a look at the u2020 project from @JakeWharton. The GalleryService retrofit interface implements such a mechanism in a very simple manner. Here's a link to the interface itself.
Here's a light example based on the u2020 project
// See how it uses a pagination index.
public interface GalleryService {
@GET("/gallery/{page}") //
Gallery listGallery(@Path("page") int page);
}
By tracking the total amount of items already downloaded from your rest service and a predefined maximum of items per page you can compute the page index necessary to call your rest service for your next set of items to download.
You can then call you rest api like this.
int nextPage = totalItemsAlreadyDownloaded / ITEMS_PER_PAGE + 1;
restApi.listGallery(nextPage);
This is a very light example based on the u2020 project but hopefully it gives you an idea of how to attack this.