Chat / iOS
Chat iOS v4
Chat iOS
Chat
iOS
Version 4
Home
/
Chat
/
iOS
/
Message

Load previous messages

Copy link

By using the loadNextPage(completionHandler:) method of a PreviousMessageListQuery instance which returns a list of BaseMessage objects, you can retrieve a set number of previous messages in a channel. With the returned list, you can display the past messages in your UI once they have loaded.

Open channel

Copy link
class CustomViewController: ViewController {
    var channel: OpenChannel?
    var query: PreviousMessageListQuery?

    func createQuery() {
        // There should be one single instance per channel.
        self.query = channel?.createPreviousMessageListQuery { params in
            // The params is the PreviousMessageListQueryParams object.
            params.customTypeFilter = "greeting"
            params.limit = LIMIT
            params.reverse = REVERSE
        }
    }

    func loadNextPage() {
        // Retrieve previous messages.
        self.query?.loadNextPage(completionHandler: { messages, error in
            guard error == nil else {
                // Handle error.
                return
            }
        })
    }
}

Group channel

Copy link
class CustomViewController: ViewController {
    var channel: GroupChannel?
    var query: PreviousMessageListQuery?

    func createQuery() {
        // There should be one single instance per channel.
        self.query = channel?.createPreviousMessageListQuery(paramsBuilder: { params in
            // The params is the PreviousMessageListQueryParams object.
            params.customTypeFilter = "greeting"
            params.limit = LIMIT
            params.reverse = REVERSE
        })
    }

    func loadNextPage() {
        // Retrieve previous messages.
        self.query?.loadNextPage(completionHandler: { messages, error in
            guard error == nil else {
                // Handle error.
                return
            }
        })
    }
}

PreviousMessageListQueryParams

Copy link
Property nameTypeDescription

limit

UInt

Specifies the number of results to return per call. Acceptable values are 1 to 100, inclusive. The recommended value for this parameter is 20.

reverse

Bool

Determines whether to sort the retrieved messages in reverse order. If set to true, messages are returned from the most recent to the oldest. (Default: false)

The limit property determines how many messages to include in a returned list. The PreviousMessageListQuery instance itself does pagination of a result set according to the value of the limit property, and internally manages a token to retrieve the next page in the result set.

Each time the loadNextPage(completionHandler:) method is called, the instance retrieves a set number of messages in the next page and then updates the token's value to complete the current call and prepare the next call. Before calling the loadNextPage(completionHandler:) method again, you should receive a success callback through the completionHandler first.

If you create a new query instance and call the loadNextPage(completionHandler:) method, a set number of the most recent messages are retrieved because its token has nothing to do with the previously created instance. So we recommend that you create a single query instance and store it as a member variable for traversing through the entire message history.