Skip to content

Commit 6a382f3

Browse files
authored
Merge pull request #4070 from woocommerce/issue/3369-media-crash-backport-6.5
[6.5 Backport] Prevent crash when the media library has exactly the same amount of items as the request page size
2 parents 43f2486 + 2b7815b commit 6a382f3

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
6.5
88
-----
99
- [*] Fix: Product images with non-latin characters in filenames now will load correctly and won't break Media Library. [https://github.com/woocommerce/woocommerce-ios/pull/3935]
10+
- [*] Fix: The screen to select images from the Media Library would sometimes crash when the library had a specific number of images. [https://github.com/woocommerce/woocommerce-ios/pull/4070]
1011

1112
6.4
1213
-----

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)