Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

MXKRoomDataSource: Synchronized Access to bubbles on Pagination #864

Merged
merged 2 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Changes to be released in next version
* MXKAccount: added generic `others` storage dictionary (vector-im/element-ios/issues/4578)

🐛 Bugfix
*
* MXKRoomDataSource: Safe access to `bubbles` array on pagination (vector-im/element-ios/issues/4605).

⚠️ API Changes
*
Expand Down
63 changes: 33 additions & 30 deletions MatrixKit/Models/Room/MXKRoomDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -1607,45 +1607,48 @@ - (void)paginateToFillRect:(CGRect)rect direction:(MXTimelineDirection)direction
CGFloat minMessageHeight = CGFLOAT_MAX;
CGFloat bubblesTotalHeight = 0;

// Check whether data has been aldready loaded
if (bubbles.count)
@synchronized(bubbles)
{
NSUInteger eventsCount = 0;
for (NSInteger i = bubbles.count - 1; i >= 0; i--)
// Check whether data has been aldready loaded
if (bubbles.count)
{
id<MXKRoomBubbleCellDataStoring> bubbleData = bubbles[i];
eventsCount += bubbleData.events.count;

CGFloat bubbleHeight = [self cellHeightAtIndex:i withMaximumWidth:rect.size.width];
// Sanity check
if (bubbleHeight)
NSUInteger eventsCount = 0;
for (NSInteger i = bubbles.count - 1; i >= 0; i--)
{
bubblesTotalHeight += bubbleHeight;

if (bubblesTotalHeight > rect.size.height)
id<MXKRoomBubbleCellDataStoring> bubbleData = bubbles[i];
eventsCount += bubbleData.events.count;

CGFloat bubbleHeight = [self cellHeightAtIndex:i withMaximumWidth:rect.size.width];
// Sanity check
if (bubbleHeight)
{
// No need to compute more cells heights, there are enough to fill the rect
MXLogDebug(@"[MXKRoomDataSource] -> %tu already loaded bubbles (%tu events) are enough to fill the screen", bubbles.count - i, eventsCount);
break;
bubblesTotalHeight += bubbleHeight;

if (bubblesTotalHeight > rect.size.height)
{
// No need to compute more cells heights, there are enough to fill the rect
MXLogDebug(@"[MXKRoomDataSource] -> %tu already loaded bubbles (%tu events) are enough to fill the screen", bubbles.count - i, eventsCount);
break;
}

// Compute the minimal height an event takes
minMessageHeight = MIN(minMessageHeight, bubbleHeight / bubbleData.events.count);
}

// Compute the minimal height an event takes
minMessageHeight = MIN(minMessageHeight, bubbleHeight / bubbleData.events.count);
}
}
}
else if (minRequestMessagesCount && [self canPaginate:direction])
{
MXLogDebug(@"[MXKRoomDataSource] paginateToFillRect: Prefill with data from the store");
// Give a chance to load data from the store before doing homeserver requests
// Reuse minRequestMessagesCount because we need to provide a number.
[self paginate:minRequestMessagesCount direction:direction onlyFromStore:YES success:^(NSUInteger addedCellNumber) {
else if (minRequestMessagesCount && [self canPaginate:direction])
{
MXLogDebug(@"[MXKRoomDataSource] paginateToFillRect: Prefill with data from the store");
// Give a chance to load data from the store before doing homeserver requests
// Reuse minRequestMessagesCount because we need to provide a number.
[self paginate:minRequestMessagesCount direction:direction onlyFromStore:YES success:^(NSUInteger addedCellNumber) {

// Then retry
[self paginateToFillRect:rect direction:direction withMinRequestMessagesCount:minRequestMessagesCount success:success failure:failure];
// Then retry
[self paginateToFillRect:rect direction:direction withMinRequestMessagesCount:minRequestMessagesCount success:success failure:failure];

} failure:failure];
return;
} failure:failure];
return;
}
}

// Is there enough cells to cover all the requested height?
Expand Down