Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid WPMediaPickerViewController performBatchUpdates crash by wrapping the call in try-catch block #412

Conversation

staskus
Copy link
Contributor

@staskus staskus commented Aug 2, 2023

Fixes wordpress-mobile/WordPress-iOS#21102

Description

WPMediaPickerViewController crashes with NSInternalInconsistencyException. The crash is not always reproducible but spiked significantly with 22.8 release.

The rise in crashes could be related to some changes in media-picker and a new collection view exception in iOS 16.4.

The proposed (short-term) solution for NSInternalInconsistencyException crashes it to wrap 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.

Link to Apple forum thread: https://developer.apple.com/forums/thread/728797?answerId=751887022#751887022

To test:

The crash is only reproducible in some of the cases, we couldn't determine an environment that would make the issue reproducible consistently.

Before

RPReplay_Final1690980668.MP4

After

RPReplay_Final1690980157.MP4

  • I have considered if this change warrants release notes and have added them to the appropriate section in the CHANGELOG.md if necessary.

staskus added 4 commits August 2, 2023 16:08
…to avoid crash

NSInternalInconsistencyException sometimes happen in cases when collection view accesses already updates data source before performBatchUpdate finishes. It can cause a crash when WPMediaPickerViewController loads.

Longer term solution would involve only updating data source in performBatchUpdate block or using DiffableDataSource
@staskus staskus marked this pull request as ready for review August 2, 2023 13:31
@kean
Copy link
Contributor

kean commented Aug 2, 2023

I tested this code yesterday by emitting completely random incremental updates, and it worked well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WPMediaPickerViewController crash: NSInternalInconsistencyException: Invalid batch updates detected
2 participants