Skip to content

Commit 676f321

Browse files
committed
Prevent crash when the media library has exactly the same amount of items as the request page size
1 parent 43f2486 commit 676f321

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

WooCommerce/Classes/ViewRelated/Products/Media/WordPressMediaLibraryPickerDataSource.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ private extension WordPressMediaLibraryPickerDataSource {
177177
/// Appends the new media items to the existing media items.
178178
///
179179
func updateMediaItems(_ newMediaItems: [Media], pageNumber: Int, pageSize: Int) {
180+
// If the response contains no new items, there is nothing to update.
181+
// We return early since the code would generate an invalid range of indices to update otherwise.
182+
guard newMediaItems.isNotEmpty else {
183+
return
184+
}
185+
180186
let pageFirstIndex = syncingCoordinator.pageFirstIndex
181187
let mediaStartIndex = (pageNumber - pageFirstIndex) * pageSize
182188
let mediaStartIndexOfTheNextPage = (pageNumber + 1 - pageFirstIndex) * pageSize

0 commit comments

Comments
 (0)