fluttercandies / flutter_photo_manager

A Flutter plugin that provides images, videos, and audio abstraction management APIs without interface integration, available on Android, iOS, macOS and OpenHarmony.
https://pub.dev/packages/photo_manager
Apache License 2.0
688 stars 314 forks source link

[BUG] Exception in iOS in `logCollections` #395

Closed lyio closed 3 years ago

lyio commented 3 years ago

Describe the bug

NSInvalidArgumentException
-[PHCollectionList canContainCustomKeyAssets]: unrecognized selector sent to instance 0x282bf3670

I am not sure what exactly is causing the problem, because I only have a stacktrace and no actual application state or steps that led to the exception.

But [PHAsset fetchKeyAssetsInAssetCollection:collection options:option] in line 104 in the PMManager.m is throwing an NSInvalidArgumentException when called with a specific PHFetchResult.

To Reproduce Again, I do not know how to trigger this exception because I only have an automated bug report.

[Edit] The problem occurs when the user tries to list assets from folders of user-defined albums. See https://developer.apple.com/documentation/photokit/phcollectionlist for a description.

Expected behavior I expect to not get an exception.

Flutter version 1.22.3

Smartphone (please complete the following information):

Log

This is the stacktrace for the exception.

OS Version: iOS 14.1 (18A8395)
Report Version: 104

Exception Type: EXC_CRASH (SIGABRT)
Crashed Thread: 1

Application Specific Information:
-[PHCollectionList canContainCustomKeyAssets]: unrecognized selector sent to instance 0x282bf3670

Thread 1 Crashed:
0   CoreFoundation                  0x1984905ac         <redacted>
1   libobjc.A.dylib                 0x33da3042c         objc_exception_throw
2   CoreFoundation                  0x19839aa2c         <redacted>
3   CoreFoundation                  0x198493130         <redacted>
4   CoreFoundation                  0x198495420         _CF_forwarding_prep_0
5   Photos                          0x339419350         <redacted>
6   Photos                          0x3393a6208         <redacted>
7   photo_manager                   0x10223fce4         -[PMManager logCollections:option:]
8   photo_manager                   0x10223fa64         -[PMManager getGalleryList:hasAll:onlyAll:option:]
9   photo_manager                   0x102248558         __26-[PMPlugin onAuth:result:]_block_invoke
10  libdispatch.dylib               0x19808cfd0         <redacted>
11  libdispatch.dylib               0x19808eac8         <redacted>
12  libdispatch.dylib               0x198091a10         <redacted>
13  libdispatch.dylib               0x19809f104         <redacted>
14  libdispatch.dylib               0x19809f8e8         <redacted>
15  libsystem_pthread.dylib         0x3630be8cc         _pthread_wqthread
16  libsystem_pthread.dylib         0x3630c577c         start_wqthread
lyio commented 3 years ago

https://stackoverflow.com/a/36732766/2419001 Seems to suggest that [PHAsset fetchKeyAssetsInAssetCollection:collection options:option] complains when it is passed something than a PHAsset.

But PHAssetCollection could either be a PHAsset or a PHCollectionList. This would mean that line 104 needs to be wrapped in a check if the collection is a PHAsset before trying to call fetchKeyAssetsInAssetCollection with it.

if ([collection isKindOfClass:[PHAssetCollection class]]) {
  PHFetchResult<PHAsset *> *result = [PHAsset fetchKeyAssetsInAssetCollection:collection options:option];
}

Edit: After looking up what a PHCollectionList is, I found this https://developer.apple.com/documentation/photokit/phcollectionlist.

A group containing Photos asset collections, such as Moments, Years, or folders of user-created albums.

So I went and created a couple of folders on my test device and put some albums in it. And now I can trigger the exception reliably every time.

I will add this information to the description above.

barfet commented 3 years ago

Faced the same issue with the same description. Waiting for the merge of the open PR, as it will fix the problem. @CaiJingLong @lyio Thanks!

lyio commented 3 years ago

@CaiJingLong since you merged the PR for this, I guess I can close the issue, right?