List all users
Get a paginated list of all users and their details using the getUsers() method.
By default, this method returns all custom user metadata without the need to define that during the call explicitly.
Requires App Context
To store data about users, you must enable App Context for your app's keyset in the Admin Portal.
Method signature
This method takes the following parameters:
1chat.getUsers(
2 filter: String? = nil,
3 sort: [PubNub.ObjectSortField] = [],
4 limit: Int?,
5 page: PubNubHashedPage? = nil
6) async throws -> (users: [UserImpl], page: PubNubHashedPage?)
Input
| Parameter | Description |
|---|---|
filterType: StringDefault: n/a | Expression used to filter the results. Returns only these users whose properties satisfy the given expression are returned. The filtering language is defined here. |
sortType: [PubNub.ObjectSortField]Default: [] | A collection to specify the sort order. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. Unless specified otherwise, the items are sorted by the last updated date. Defaults to an empty list. |
limitType: IntDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100. |
pageType: PubNubHashedPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
Output
| Parameter | Description |
|---|---|
[UserImpl] | Object containing a set of users with pagination information (next, prev, total). |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Fetch all existing user IDs.
1// Assuming you have a reference of type "ChatImpl" named "chat"
2Task {
3 // Fetch the first set of users.
4 // There's no need to pass the "page" parameter, as it's nil by default, indicating the first page
5 let getUsersResult = try await chat.getUsers()
6 debugPrint("Fetched channels: \(getUsersResult.users)")
7 debugPrint("Next page: \(String(describing: getUsersResult.page))")
8
9 // The example below shows how to read the next page from the previous response and pass it to a subsequent call.
10 // You can recursively keep fetching using the page objects from previous responses until the returned array is empty
11 if let nextPage = getUsersResult.page, !getUsersResult.users.isEmpty {
12 let resultsFromNextPage = try await chat.getUsers(page: nextPage)
13 }
14}
Other examples
Pagination
Get the total number of 25 users and then specify that you want to fetch the results from the next page using a page that was previously returned from the PubNub server.
1// Assuming you have a reference of type "ChatImpl" named "chat" and the "page" reference of "PubNubHashedPage" type
2Task {
3 let getUsersResult = try await chat.getUsers(limit: 25, page: page)
4 debugPrint("Fetched channels: \(getUsersResult.users)")
5 debugPrint("Next page: \(String(describing: getUsersResult.page))")
6
7 // The example below shows how to read the next page and pass it to a subsequent call.
8 // You can recursively keep fetching using the page objects from previous responses until the returned array is empty
9 if let nextPage = getUsersResult.page, !getUsersResult.users.isEmpty {
10 let resultsFromNextPage = try await chat.getUsers(limit: 25, page: nextPage)
11 }
12}
Archived users
Get all archived users. This request will return all users removed with the soft option set to true, whose data is still stored in the App Context storage.
1// Assuming you have a reference of type "ChatImpl" named "chat"
2Task {
3 // Fetch the first set of users.
4 // There's no need to pass the "page" parameter, as it's nil by default, indicating the first page
5 let getUsersResult = try await chat.getUsers(filter: "status == 'deleted'", limit: 25)
6 debugPrint("Fetched channels: \(getUsersResult.users)")
7 debugPrint("Next page: \(String(describing: getUsersResult.page))")
8
9 // The example below shows how to read the next page and pass it to a subsequent call.
10 // You can recursively keep fetching using the page objects from previous responses until the returned array is empty
11 if let nextPage = getUsersResult.page, !getUsersResult.users.isEmpty {
12 let resultsFromNextPage = try await chat.getUsers(filter: "status == 'deleted'", limit: 25, page: nextPage)
13 }
14}