From the course: Building Android Apps with AWS

Perform database queries

From the course: Building Android Apps with AWS

Start my 1-month free trial

Perform database queries

- [Instructor] The next thing that I'm gonna do is create some database queries. And the two types of queries that we need to perform in our demo app here, are the query for retrieving the list names and the query for retrieving all of the items on a given list and so right now both of those methods are in our ListManager class and they're called requestListNames and requestListItems and so we're gonna update both of those methods so that they get the data from DynamoDB instead of the shared preferences. And in the interest of time, I've actually included a text file called query_methods in our exercise files and I'm just gonna copy all of the content for these two methods out of that text file. So you can just copy all of the contents of this text file in its entirety. So I'm gonna copy everything like that and then we can come in and I'm just going to overwrite both of those methods. Now I quickly wanna make sure that all of the classes get imported. So I'm just gonna run through and import the necessary classes. Make sure that condition gets imported, the comparison operator and the attribute value and then I just want to talk through what's going on in these methods. So I'll start by taking a look at the request list names method and at line 48 it's starting by getting a reference to our DynamoDB mapper object from the AWS provider and then in line 51, it's using the identity manager to retrieve the user ID for the user that's currently logged in. And in line 54, it's creating a new list names DO object, used as a template and it's assigning the user ID property of that object. And the reason that we're doing that is so that in line 58 we can create a DynamoDB query expression and we pass it that template object that we created. What that's doing is it's going to create a query that will look for all of the entries in the list names table that match this template. So all of the entries that have a matching user ID. And then in line 61, we're using the DB mapper object to simply perform the query. And we're passing it this property to tell it that we want it to return list names DO objects. So that's what's gonna be returned by this query. And then in line 64, it's simply iterating over the results and adding them to our local data model and then finally sending out the broadcast to say that all of the list names have been downloaded. And now if we scroll down and take a look at the request list items method, it starts off in very similar manner. It also requests the DB mapper object. Also gets a reference to the user ID, by using the identity manager and it also creates a template object. This time it's a list items DO object, but it creates that template and it assigns the user ID property of that template object. But now we have this additional step, because in order to make this request, we wanna find list items that not only have a matching user ID, but they also need to belong to the list that's been provided. So it creates what's called a condition object and this condition object gets provided a type of comparison. So this means that we're looking for two things that are equal and the particular attribute that we want it to be equal with, is the name ID property of our provided list. So down here at line 94, when it's creating the DynamoDB query expression, it passes it the same template object, but additionally, we pass it this condition. So what we're saying is that we want the list name ID attribute to be equal to the name ID attribute of the given list. So then in line 100, it's simply using the DB mapper again to execute the query and it's gonna return an array of list items DO objects. And then in line 103 we simply iterate over that list and add them to our local data model and when that's all done, it sends out a broadcast. So now both of these query methods are retrieving the data from our DynamoDB database instead of the shared preferences and the last thing that I wanna point out is to make sure you remember that these calls need to happen on a background thread and in our demo here, the list maker application is calling these methods using background tasks, so these calls are all happening in the background.

Contents