Chat / iOS
Chat iOS v4
Chat iOS
Chat
iOS
Home
/
Chat
/
iOS
/
Message
This is the new Docs for Chat SDK v4 for iOS. To see the previous Docs, click here.

Load previous messages

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

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

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

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 a next call.

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.

Note: Before calling the loadNextPage(completionHandler:) method again, you should receive a success callback through the completionHandler first.