Front-End Web & Mobile
Using Amazon DynamoDB Document API with the AWS Mobile SDK for Android – Part 2
In the first part of this blog, we introduced you to the Amazon DynamoDB Document API. This API is a mechanism for accessing data in DynamoDB that doesn’t require you to map models to the data. Instead, you access the data through standard accessor methods on a standard Document object. In this blog post, we show how to integrate this functionality into a real application. The application for this demonstration is an Android memo app.
The mobile backend architecture for the app is shown in the following diagram.
The services used are:
- Amazon Cognito, which authenticated the mobile device to the backend.
- AWS Identity and Access Management (IAM), which defines permissions for the mobile device.
- Amazon DynamoDB, which stores the data for the individual memos.
This infrastructure can be easily set up using AWS Mobile Hub. Follow these instructions:
- Click Create a new Project in the AWS Mobile Hub console. Provide a name for your project.
- Click NoSQL Database.
- Click Enable NoSQL.
- Click Add a new table.
- Click Start with an example schema.
- Select Notes as the example schema.
- Select Public for the permissions (we won’t add sign-in code to this app).
- Click Create Table, and then click Create Table in the dialog box.
Even though the table you created has a userID, the data is stored unauthenticated in this example. If you were to use this app in production, use Amazon Cognito to sign the user in to your app, and then use the userID to store the authenticated data.
In the left navigation pane, click Resources. AWS Mobile Hub created an Amazon Cognito identity pool, an IAM role, and a DynamoDB database for your project. Mobile Hub also linked the three resources according to the permissions you selected when you created the table. For this demo, you need the following information:
- The Amazon Cognito identity pool ID (for example, us-east-1:f9d582af-51f9-4db3-8e36-7bdf25f4ee07)
- The name of the Notes table (for example, androidmemoapp-mobilehub-1932532734-Notes)
These are stored in the application code and used when connecting to the database.
Now that you have a mobile app backend, it’s time to look at the frontend. First, add the required libraries to the dependencies section of the application build.gradle file:
Add the INTERNET, ACCESS_NETWORK_STATE, and ACCESS_WIFI_STATE permissions to AndroidManifest.xml:
Set up the connection to the Amazon DynamoDB table by creating an Amazon Cognito credentials provider (for appropriate access permissions), and then creating a DynamoDbClient object. Finally, create a table reference:
You can now perform CRUD (Create, Read, Update, Delete) operations on the table:
There are two mechanisms for searching the dataset: scan and query. The query() method uses indexed fields within the DynamoDB table to rapidly retrieve the appropriate information. The scan() method is more flexible. It allows you to search on every field, but it can run into performance issues when searching large amounts of data. This results in a worse experience for your users because data retrieval will be slower. For the best experience, index fields that you intend to search often and use the query() method.
The Notes schema in DynamoDB usually segments data on a per-user basis. The app works with both authenticated and unauthenticated users by using the .getCachedIdentityId() method. This method stores the current user identity with every new note that is created.
Android does not allow you to perform network requests on the main UI thread. You must wrap each operation in an AsyncTask. For example:
You can initiate a save operation by instantiating the appropriate AsyncTask and then calling .execute():
Similarly, you can retrieve a list of memos on an AsyncTask and pass the memos back to a method in the MainActivity to populate the UI:
The Document API is currently in beta. We would like to hear about the use cases you want us to support. You can leave your feedback and issues on our forums and GitHub.