iOS: Message Browser Next Page for Conversation
I have an iOS chat app where I retrieve the latest X messages from a given Conversation using the messages browser. I am now trying to retrieve the next page of X messages from the message browser using its "nextPage" method but this always returns an empty message list even though the "hasMorePages" property is always true.
Is this the correct way to retrieve the next page of messages or is there another way?
iOS SDK version 1.72.0
-
The general pattern to browse messages from a conversation is :
-(void) loadMessages {
if (self.conversation.isSynchronized && ![self.messagesBrowser hasMorePages]){
NSLog(@"This conversation is already synchronized and we are not loading a new page");
BOOL hasPage = [_messagesBrowser hasMorePages];
// show that there are more pages to load if hasPage is true or use pull to refresh...
} else if(!_conversation.isSynchronized){
NSLog (@"loadMessages : start resync");
self.isRetrievingNextPage = YES;
[self.messagesBrowser resyncBrowsingCacheWithCompletionHandler:^(NSArray *addedCacheItems, NSArray *removedCacheItems, NSArray *updatedCacheItems, NSError *error) {
NSLog(@"Messages resync done");
BOOL hasPage = [self.messagesBrowser hasMorePages];
self.isRetrievingNextPage = NO;
if (hasPage && ...) { // check for example pull to refresh
[self loadMessages];
}
}];
} else {
OTCLog(@"We have scrolled so load next page");
self.isRetrievingNextPage = YES;
[self.messagesBrowser nextPageWithCompletionHandler:^(NSArray *addedCacheItems, NSArray *removedCacheItems, NSArray *updatedCacheItems, NSError *error) {
if(!error){
BOOL hasPage = [_messagesBrowser hasMorePages];
self.isRetrievingNextPage = NO;
}];
}
} -
Hi Vladimir,
Thank you for your reply and sample code. I am using Swift and the Conversation object has no property called 'isSynchronised'.
What I see is that when I call the resyncBrowsingCache method, it returns an error saying "Conversation is already synchronized", but I have no way of knowing this without calling that method first. If I then call nextPage, there is no error but the cached item arrays are all empty (which I guess is expected if the conversation is synchronised).
My flow is: If message browser has more pages, call resyncBrowsingCache followed by nextPage.
The hasMorePages property of my message browser is always true and resyncBrowsingCache always returns the error saying that the conversation is already synchronised. I am using a page size of 20 and there are a lot more messages in the conversation so it seems strange that the SDK is always reporting the conversation to be synchronised.
If I call the nextPage method first and then resyncBrowsingCache, I get the same behaviour. If I use a page size greater than the total number of messages in the conversation, I still get the same behaviour.
-
Hi Rick,
I just added a "load more" button in the ChatViewController in the Swift demo app :
https://github.com/Rainbow-CPaaS/Rainbow-iOS-SDK-Samples/tree/master/Rainbow-iOS-SDK-Sample-Swift
You should see if the way it is working could solve your issue.
-
Hi Vladimir,
Thank you for the update to the sample code. At first glance, my code matched yours so I was still confused as to why it wasn't working. Then I discovered that the issue was caused by the fact that I was recreating the message browser rather than instantiating it once and using that same reference to retrieve subsequent pages of the conversation.
So, issue solved. Thank you for your help, it's much appreciated.
Rick
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
4 Kommentare