Welcome to this Android app development tutorial, where we show you a more efficient way of searching users using powerful sort operators. In Android app development, there are many repetitive routine tasks that are nonetheless necessary for enhancing your app. Creating functionality for searching users, for example, is a common task. Searching users is an important part of business logic and provides many benefits for the app user, but for the developer this routine task can seem tedious and takes their focus away from more interesting aspects to app development. For this tutorial we will show you how to search users in a more efficient way when using the Android QuickBlox SDK. Our mission is to streamline this process, reducing development time, and offering a lucid understanding of this seemingly straightforward yet crucial use case.
The ability to search users in an app is a valuable feature that enhances user experience and optimizes the management of data within an application. For a service provider, who wants to track information about their app users, being able to group them according to different criteria offers numerous benefits.
In the context of Android app development, when searching users or other types of data, developers often use sort operators to order the information as needed. “Search operators” generally refer to programming constructs or functions that facilitate the searching of data in a specific order.
In this tutorial, we will utilize QuickBlox Android SDK, a powerful toolkit for Android developers, empowering them to seamlessly integrate real-time communication functionalities into their applications.
In regards to searching users there are several unusual scenarios which don’t have native implementations in QuickBlox Android SDK. We will focus on these in the following and show you some quick efficient ways of using search operators.
When using the QuickBlox Android SDK, users can be fetched based on the following criteria.
Let’s give you some examples of how and when these criteria could be implemented.
App owners will want to track the number of new enrollments, with the ability to find out how many users registered on the app on any given date. For this scenario they can implement use case 1 – Load users created in a specific time period.
There may be a situation when an organization needs to know which app users updated their app profile within a specific time period, perhaps they want to send them notifications of a special promotion or offer. This list can be achieved by use case 2 – Load users which updated in a specific time period.
It may be necessary to notify older long-term app users when the app owner updates certain policies or procedures. This cohort of older users can be fetched by use case 3 – Load users created before exact date.
Alternatively, that same app owner may want to notify new app users who signed up within the last 7 days about onboarding information. In this scenario they can implement use case number 4 – Load users created after exact date.
What if app users fail to update their profiles or provide certain information. The app owner might want to send them a push notification reminding them to update. They can fetch a list of inactive users by use case number 5 – Load users who updated before exact date.
Alternatively, the app owner might need to know which users already updated their profiles. For this task, they can use use case number 6 – Load users who updated after exact date.
Lastly, there may be a scenario when we need to exclude some users from our results. For example, an organization may want to implement a black list or needs to select all customers but exclude service providers. In this case, they can implement use case 7 – Load users excluding specific email, phone number or login.
To streamline the process of searching users we have prepared some ready code snippets for you. But first, for creating the custom requests for searching we need to provide some additional parameters to Query by using GenericQueryRule. Let’s see how we can do it by code snippet below:
String field = "created_at"; String typeField = "date"; String paramFilter = "filter[]"; String searchOperator = "between"; String searchValue = "2021-01-01, 2021-05-06"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue);
String field = "created_at";
The string field variable allows us to set the parameters for sorting and filtering our users. In the example above we have selected the value “created_at”, which means we will filter users created on a specific date. However, other possible values could be: id, created_at
, updated_at
, last_request_at
, external_user_id
, facebook_id
.
String typeField = "date";
In this next variable, “typeField” we set the type of data we are using for searching in our “field”. In this case we set “date,” but other possible values could be, number or string. For example “created_at,” “updated_at,” “last_request_at,” each require the data type, “date,” but “external_user_id,” and “facebook_id” require the data type, “number.”
String paramFilter = "filter[]";
The field “paramFilter” has the value “filter[]”. It’s specially reserved words for the backend to notify about our intent. In this case our intent is to load users by some criteria. In other words, it says “Hey server, I want to load users and filter them by some specific criteria.”
String searchOperator = "between";
Field “searchOperator” has the value “between,” which means it will search for users between two parameters, in our case between two dates. The possible values could be:
gt – greater than
lt – less than
ge – greater or equal to
le – less or equal to
eq – equal
ne – not equal
between – between two values
String searchValue = "2021-01-01, 2021-05-06";
The variable, “searchValue,” allows us to set the exact values we need to search between or for. In the examples above, all users which were created between 2021-01-10 and 2021-05-06 will be identified and listed.
GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue);
The code snippet above reveals our final QueryRule. Here we have paramFilter, typeField, field, searchOperator and searchValue.
At the end, if we look inside the source of our request it will have the following format:
filter[]=date created_at between 2021-01-01, 2021-05-06
What this means, essentially, is “I need to filter by “date” data type, by field “created_at,” which values exist in between “2021-01-01, 2021-05-06”.
Please see our official documentation to find more information about List Users and Filter/Sort Operators.
Now let’s back to our use cases, and we can show you how they can be implemented in the code.
String field = "created_at"; String typeField = "date"; String paramFilter = "filter[]"; String searchOperator = "between"; String searchValue = "2021-01-01, 2021-05-06"; //our time period GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "updated_at"; String typeField = "date"; String paramFilter = "filter[]"; String searchOperator = "between"; String searchValue = "2021-01-01, 2021-05-06"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "created_at"; String typeField = "date"; String paramFilter = "filter[]"; String searchOperator = "lt"; String searchValue = "2021-05-06"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "created_at"; String typeField = "date"; String paramFilter = "filter[]"; String searchOperator = "gt"; String searchValue = "2021-05-06"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "updated_at"; String typeField = "date"; String paramFilter = "filter[]"; String searchOperator = "le"; String searchValue = "2021-05-06"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "updated_at"; String typeField = "date"; String paramFilter = "filter[]"; String searchOperator = "ge"; String searchValue = "2021-05-06"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "email"; String typeField = "string"; String paramFilter = "filter[]"; String searchOperator = "ne"; String searchValue = "test@test.com"; //our email which you will exclude GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "phone"; String typeField = "number"; String paramFilter = "filter[]"; String searchOperator = "ne"; String searchValue = "831882113"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
String field = "login"; String typeField = "string"; String paramFilter = "filter[]"; String searchOperator = "ne"; String searchValue = "testUser"; GenericQueryRule queryRule = new GenericQueryRule(paramFilter, ((typeField + " " + field) + " " + searchOperator) + " " + searchValue); ArrayList<GenericQueryRule> rules = new ArrayList<>(); rules.add(queryRule); QBPagedRequestBuilder requestBuilder = new QBPagedRequestBuilder(); requestBuilder.setRules(rules); QBUsers.getUsers(requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() { @Override public void onSuccess(ArrayList<QBUser> result, Bundle params) { // handle result } @Override public void onError(QBResponseException responseException) { // handle error } });
Our aim in this article has been to help streamline the process of searching users with search operators when developing apps with the QuickBlox Android SDK. By integrating these newfound efficiencies, you now possess the tools to navigate these tasks more flexibly and efficiently. This not only stands to enhance your overall business operations but also holds the promise of a substantial reduction in development time, allowing you to focus on more innovative and impactful aspects of your application.
Please leave your comments below if you have any further questions, or join our Discord developer community!