Hi, we are currently implementing a similar version to the CouchChat Demo app and struggle with a small detail. We want to limit the amount chat messages displayed in order to reduce the memory footprint. Similar to the iMessage app we want to add a "load earlier messages" feature that loads additional (older) messages on demand.
From a high-level perspective we think it might make sense to have a global variable, lets say _messageCount, that starts with 25 and increases by 25 each time the user taps "load earlier messages". It seems very easy to do but we struggle with the query logic.
Looking at the CouchChat app we can run the query and limit the output but the limit always starts at the oldest message and not the newest one obviously. Using start key and limit returns nothing and so does descending order = YES.
Does anyone have any pointers to how to get this accomplished?
Thanks Christoph
Building the query
#pragma mark - MESSAGES:
- (CBLQuery*) chatMessagesQuery
{
CBLQuery* query = [[self.database viewNamed: @"chatMessages"] createQuery];
query.startKey = @[self.chatID];
query.endKey = @[self.chatID, @{}];
query.mapOnly = true;
return query;
}
//it doesn't work as it limits the scope to the first 25messages
- (void) reloadFromQuery
{
//set initial _messageLimit to 25
_query.limit = _messageLimit;
CBLQueryEnumerator *rowEnum = _query.rows;
if (rowEnum)
{
_rows = rowEnum.allObjects;
[self.collectionView reloadData];
[self scrollToBottomAnimated:YES];
[_chatRoom markAsRead];
}
}
//it doesn't work as it returns nil
- (void) reloadFromQuery
{
//set initial _messageLimit to 25
//_totalAmount of rows
_query.descending = YES;
_query.startKey = _totalAmount;
_query.endKey = _totalAmount - _messageLimit;
CBLQueryEnumerator *rowEnum = _query.rows;
if (rowEnum)
{
_rows = rowEnum.allObjects;
[self.collectionView reloadData];
[self scrollToBottomAnimated:YES];
[_chatRoom markAsRead];
}
}