Search code examples
iosswiftinfinite-scrollpubnubjsqmessagesviewcontroller

Swift + Pubnub chat app load old message for scrolling


In viewDidLoad of my chat view controller, I wrote self.appDelegate.client?.historyForChannel(currentChannel, start: nil, end: nil, limit: 20, withCompletion: and it retrieves the 20 recent messages. However, I wish to retrieve earlier/old 20 messages before these recent 20 messages for my infinite scrolling feature. How can I do this?


Solution

  • Store the timestamp of the first message you received from Pubnub history, to receive next 20 messages:

    self.client?.historyForChannel(channel, start: lastStoredTimstamp, end: nil, limit: 20, reverse: false, withCompletion:

    I have tested it and it works well.

    Little Description: Using only a start parameter always returns messages older than the time token provided. If you set reverse = true, you will get message newer than the time token provided.

    Please see https://www.pubnub.com/docs/swift/storage-and-history how the PubNub history API works with timeline diagrams.