Sendbird SyncManager channel pagination issue

264 Views Asked by At

I am using Sendbird for chat messages in an native iOS app. They have an add-on SyncManager. Which caches the channel list and messages locally on the phone for offline support.

I am trying to paginate the channel list using SyncManager's collection. Here is my code snippet:

func fetchCollection() {
    self.query = SBDGroupChannel.createMyGroupChannelListQuery()
    self.query?.order = .latestLastMessage
    self.query?.limit = 20
    
    self.collection = SBSMChannelCollection(query: query)
    self.collection?.delegate = self
}

func fetchNextPage() {
    // Problem: The `query.hasNext` is never set to `false`!
    guard let query = query, query.hasNext else { return }
    collection?.fetch { error in
        if let error = error {
            self.log("Error: \(error)", level: .debug)
            return
        }

        // Do nothing, as the collection delegate would be notified to the delegate.
    }
}
    
func collection(_ collection: SBSMChannelCollection, didReceiveEvent action: SBSMChannelEventAction, channels: [SBDGroupChannel]) {
    switch action {
    case .insert:
        datasource.insertChannels(channels)
    case .update:
        datasource.moveChannels(channels)
    case .remove:
        datasource.deleteChannels(channels)
    case .move:
        datasource.moveChannels(channels)
    case .clear:
        datasource.clearChannels()
    case .none:
        DispatchQueue.main.async { [weak self] in
            self?.tableView.reloadData()
        }

    default:
        assertionFailure("Undefine action")
        DispatchQueue.main.async { [weak self] in
            self?.tableView.reloadData()
        }
    }
}

The problem is the query.hasNext is never set to false. I have tried checking collection.query.hasNext as well, but that too has true always.

Note that Sendbird's basic SDK does set the query.hasNext to false and hence I can determine whether or not the query has next page and I should make another call. I want to paginate using the SyncManager.

Their sample app for SyncManager sends the next page request without checking if there is a next page available, so every time the user scrolls to bottom, they are calling the API. Am I missing anything here?

1

There are 1 best solutions below

1
On

@sunil At the moment you are building a new query each time you invoke fetchNextPage. You need to maintain the original query in order for hasNext to be true.