Free trial *Internet Service Required

Refine Mobile Services queries with paging

This topic shows you how to use paging to manage the amount of data returned to your Android app from Windows Azure Mobile Services. In this tutorial, you will use the top and skip query methods on the client to request specific "pages" of data.

Note

To prevent data overflow in mobile device clients, Mobile Services implements an automatic page limit, which defaults to a maximum of 50 items in a response. By specifying the page size, you can explicitly request up to 1,000 items in the response.

This tutorial builds on the steps and the sample app from the previous tutorial Get started with data. Before you begin this tutorial, you must complete at least the first tutorial in the working with data series, Get started with data.

  1. In Eclipse, open the project that you created when you completed the tutorial Get started with data.

  2. From the Run menu, then click Run to start the app, then enter text into the textbox and click the Add button.

  3. Repeat the previous step at least three times, so that you have more than three items stored in the TodoItem table.

  4. In the ToDoActivity.java file, replace the RefreshTodoItems method with the following code:

    private void refreshItemsFromTable() {
        // Define a filtered query that returns the top 3 items.
        mToDoTable.where().field("complete").eq(false).top(3)
                .execute(new TableQueryCallback<ToDoItem>() {
    
    
                public void onCompleted(List<ToDoItem> result, int count,
                        Exception exception, ServiceFilterResponse response) {
                    if (exception == null) {
                        mAdapter.clear();
    
    
                        for (ToDoItem item : result) {
                            mAdapter.add(item);
                        }
    
    
                    } else {
                        createAndShowDialog(exception, "Error");
                    }
                }
            });
    
    
    }

    This query returns the top three items that are not marked as completed.

  5. Rebuild and start the app.

    Notice that only the first three results from the TodoItem table are displayed.

  6. (Optional) View the URI of the request sent to the mobile service by using message inspection software, such as browser developer tools or Fiddler.

    Notice that the top(3) method was translated into the query option $top=3 in the query URI.

  7. Update the RefreshTodoItems method once more with the following code:

    private void refreshItemsFromTable() {
        // Define a filtered query that returns the top 3 items.
        mToDoTable.where().field("complete").eq(false).skip(3).top(3)
                .execute(new TableQueryCallback<ToDoItem>() {
    
    
                public void onCompleted(List<ToDoItem> result, int count,
                        Exception exception, ServiceFilterResponse response) {
                    if (exception == null) {
                        mAdapter.clear();
    
    
                        for (ToDoItem item : result) {
                            mAdapter.add(item);
                        }
    
    
                    } else {
                        createAndShowDialog(exception, "Error");
                    }
                }
            });
    
    
    }

    This query skips the first three results and returns the next three after that. This is effectively the second "page" of data, where the page size is three items.

    Note

    This tutorial uses a simplified scenario by passing hard-coded paging values to the top and skip methods. In a real-world app, you can use queries similar to the above with a pager control or comparable UI to let users navigate to previous and next pages. You can also call the includeInlineCount method to get the total count of items available on the server, along with the paged data.

  8. (Optional) Again view the URI of the request sent to the mobile service.

    Notice that the skip(3) method was translated into the query option $skip=3 in the query URI.

Next Steps

This concludes the set of tutorials that demonstrate the basics of working with data in Mobile Services. Consider finding out more about the following Mobile Services topics:

Rss Newsletter