==

Q-Consultation for every industry

Securely hold virtual meetings and video conferences

Learn More>

Want to learn more about our products and services?

Speak to us now

How to sort users efficiently in your apps using QuickBlox Android SDK

Kirill Tolmachev
12 Feb 2024
QuickBlox Android SDK

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 importance of searching Users

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.

Examples of Searching Criteria

When using the QuickBlox Android SDK, users can be fetched based on the following criteria.

  1. created in specific time period
  2. updated in specific time period
  3. created before exact date
  4. created after exact date
  5. updated before exact date
  6. updated after exact date
  7. exclude exact email, phone number, or login

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.

Need the code snippet? We have it for you!

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.

Code Snippets for User Cases

Now let’s back to our use cases, and we can show you how they can be implemented in the code.

Load users created in specific time period

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
   }
});

Load users who updated in specific time period

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
   }
});

Load users created before exact date

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
   }
});

Load users created after exact date

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
   }
});

Load users updated before exact date

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
   }
});

Load users updated after exact date

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
   }
});

Load users which exclude exact email

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
   }
});

Load users which exclude exact phone number

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
   }
});

Load users which exclude exact phone login

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
   }
});

Wrapping up

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!

Have Questions? Need Support?

Join the QuickBlox Developer Discord Community, where you can share ideas, learn about our software, & get support.

Join QuickBlox Discord

Read More

Ready to get started?

QUICKBLOX
QuickBlox post-box