Retrieve Users

Learn how to retrieve a list of users.

Retrieve Logged In User Details

You can get the details of the logged-in user using the getLoggedInUser() method. This method can also be used to check if the user is logged in or not. If the method returns nil, it indicates that the user is not logged in and you need to log the user into CometChat SDK.

let user = CometChat.getLoggedInUser();
User *user = [CometChat getLoggedInUser];

This method will return a User object containing all the information related to the logged-in user.

Retrieve List of Users

In order to fetch the list of users, you can use the UsersRequest class. To use this class i.e to create an object of the UsersRequest class, you need to use the UsersRequestBuilder class. The UsersRequestBuilder class allows you to set the parameters based on which the users are to be fetched.

The UsersRequestBuilder class allows you to set the below parameters:

  1. set(limit: Int) - This method sets the limit i.e. the number of users that should be fetched in a single iteration.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.build();

  1. set(searchKeyword: String) - This method allows you to set the search string based on which the users are to be fetched.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.set(searchKeyword: "abc")
.build();

  1. set(status: CometChat.UserStatus) - The status based on which the users are to be fetched. The status parameter can contain one of the below two values:
    a. CometChat.UserStatus.online - will return the list of only online users.
    b. CometChat.UserStatus.offline - will return the list of only offline users.
    If this parameter is not set, will return all the available users.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.set(status: .online)
.build();

  1. hideBlockedUsers(hideBlockedUsers: Bool) - This method is used to determine if the blocked users should be returned as a part of the user list. if set to true, the user list will not contain the users blocked by the logged-in user.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.hideBlockedUsers(true)
.build();

  1. set(role: String) - This method when set will only return the users that belong to the role specified.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.set(role : "admin")
.build();

❗️

Important

Please note that setRole() method has been deprecated starting version 2.1.2. Please use setRoles() method instead.

  1. set(roles:[String]) - This method allows you to fetch the users based on multiple roles.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.set(roles : ["role1","role2"])
.build();

  1. friendsOnly(boolean friendsOnly) - This property when set to true will return only the friends of the logged-in user.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.friendsOnly(true)
.build();
  1. set(tags: [String]) - This method accepts a list of tags based on which the list of users is to be fetched. The list fetched will only contain the users that have been tagged with the specified tags.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.set(tags: ["tag1", "tag2"])
.build();
  1. withTags(boolean withTags) - This property when set to true will fetch tags data along with the list of users.
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: 30)
.withTags(true)
.build();

  1. set(UIDs: [String]) - This method accepts a list of UIDs based on which the list of users is fetched. A maximum of 25 users can be fetched.
let UIDs = ["superhero1", "superhero2"]
let usersRequest = UsersRequest.UsersRequestBuilder()
.set(limit: limit)
.setUIDs(UIDs)
.build();

Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the UsersRequest class.

Once you have the object of the UsersRequest class, you need to call the fetchNext() method. Calling this method will return a list of User objects containing n number of users where n is the limit set in the builder class.

let limit = 20;

let usersRequest = UsersRequest.UsersRequestBuilder(limit: limit).build();

usersRequest.fetchNext(onSuccess: { (users) in

  for user in users {

     print("User: " + user.stringValue())
  }

}) { (error) in

  print("User list fetching failed with error: " + error!.errorDescription);
}
NSInteger limit = 30 ;

UsersRequest *userRequest = [[[UsersRequestBuilder alloc]initWithLimit:limit] build];

[userRequest fetchNextOnSuccess:^(NSArray<User *> * users) {
    
    for (User *user in users) {

      NSLog(@"User: %@ ",[user stringValue]);
    }
    
} onError:^(CometChatException * error) {
    
    NSLog(@"User list fetching failed with error: %@",[error errorDescription]);
    
}];

Retrieve Particular User Details

To get the information of a user, you can use the getUser() method.

let uid  = "superhero1";

CometChat.getUser(UID: uid, onSuccess: { (user) in

  print("User: " + user.stringValue())

}) { (error) in

  print("User fetching failed with error: " + error.errorDescription);
}
NSString *UID = @"SUPERHERO01";

[CometChat getUserWithUID:UID onSuccess:^(User * user) {
    
    NSLog(@"User: %@",[user stringValue]);
    
} onError:^(CometChatException * error) {
    
    NSLog(@"User fetching failed with error: %@",[error errorDescription]);
}];

The getUser() method takes the following parameters:

Parameter

Description

UID

The UID of the user for whom the details are to be fetched.

On success, the You will receive a User objects.

Get online user count

To get the total count of online users for your app, you can use the getOnlineUserCount() method.

CometChat.getOnlineUserCount(onSuccess: {
    count in
  print("Online Users count : ",count)
}, onError: {
  error in
  print(error?.errorDescription)
})

This method returns the total online user count for your app.


Did this page help you?