Skip to content

Commit

Permalink
Merge pull request #412 from wordpress-mobile/fix/wpmediapickerviewco…
Browse files Browse the repository at this point in the history
…ntroller-nsinternalinconsistencyexception-crash

Avoid `WPMediaPickerViewController` `performBatchUpdates` crash by wrapping the call in try-catch block
  • Loading branch information
staskus authored Aug 2, 2023
2 parents b66ddcd + aae7aa6 commit 68663b1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ _None._

_None._

## 1.8.9

### Bug Fixes

- Fix WPMediaPickerViewController crash when performing batch updates [#412]

## 1.8.8

### Bug Fixes
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- WPMediaPicker (1.8.8)
- WPMediaPicker (1.8.9-beta.1)

DEPENDENCIES:
- WPMediaPicker (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
WPMediaPicker: 0d40b8d66b6dfdaa2d6a41e3be51249ff5898775
WPMediaPicker: 0ef7f4abcbff7ad20e271e7d09586e32924f5785

PODFILE CHECKSUM: 31590cb12765a73c9da27d6ea5b8b127c095d71d

Expand Down
19 changes: 18 additions & 1 deletion Pod/Classes/WPMediaPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,24 @@ - (void)registerDataSourceObservers {
return;
}
if (incrementalChanges) {
[weakSelf updateDataWithRemoved:removed inserted:inserted changed:changed moved:moves];
/// Avoid NSInternalInconsistencyException crash by wrapping performBatchUpdates in the try catch block.
///
/// Apple documentation indicates if the collection view’s layout isn’t up to date before you call performBatchUpdates,
/// additional reload may occur that can cause problems. Developers should update the data model inside the updates
/// block or ensure the layout is updated before calling performBatchUpdates.
/// However, MediaLibraryPickerDataSource reloads the data source before view controller gets informed about updates,
/// creating a possibility for a crash.
/// https://developer.apple.com/documentation/uikit/uicollectionview/1618045-performbatchupdates
///
/// Apple engineers reiterate this fact and point out the best way to avoid this issue is to adopt
/// UICollectionViewDiffableDataSource which requires refactoring of the current solution
/// https://developer.apple.com/forums/thread/728797?answerId=751887022#751887022

@try {
[weakSelf updateDataWithRemoved:removed inserted:inserted changed:changed moved:moves];
} @catch (NSException *exception) {
[weakSelf.collectionView reloadData];
}
} else {
[weakSelf.collectionView reloadData];
}
Expand Down
2 changes: 1 addition & 1 deletion WPMediaPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Pod::Spec.new do |s|
s.name = 'WPMediaPicker'
s.version = '1.8.8'
s.version = '1.8.9-beta.1'

s.summary = 'WPMediaPicker is an iOS controller that allows capture and picking of media assets.'
s.description = <<-DESC
Expand Down

0 comments on commit 68663b1

Please sign in to comment.