app crashed when creating album

The app crashes when creating a new album. This crash did not occur in our own testing, but after publishing it to the app store, it seems that the probability of occurrence is very high.

How are we supposed to help you with this? What's an album? What code are you running that's causing the crash?

__block PHObjectPlaceholder *placeholder = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
    placeholder = request.placeholderForCreatedAssetCollection;
} completionHandler:^(BOOL success, NSError *error) {
    if (resultBlock) {
        PhotoAlbum *resultAlbum = nil;
        if (placeholder) {
            PHFetchResult<PHAssetCollection *> *collectionResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[
                placeholder.localIdentifier
                ] options:nil];
            if (collectionResult.count) {
                resultAlbum = [[PhotoAlbum alloc] initWithPHAssetCollection:[collectionResult firstObject]
                                                                        options:[self assetFilterFetchOptions:self.fetchOption]];
            } else {
                success = NO;
                error = [NSError errorWithDomain:kDomain code:AlbumNoFound userInfo:@{@"error" : [NSString stringWithFormat:@"没有找到标题为%@的相册", title]}];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            resultBlock(success, error, resultAlbum);
        });
    }
}];

crash in this:

PHFetchResult<PHAssetCollection *> *collectionResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[
                placeholder.localIdentifier
                ] options:nil];

Hi QiuZH,

The completion handler is called on an arbitrary queue, and this exception may be arising because it's not currently safe to call other PhotoKit API directly on that queue. In your completion handler, try dispatching to some other known queue (e.g. the main queue or a private queue that you create) before calling additional PhotoKit methods.

Even the call to -[self assetFilterFetchOptions:] may not be safe to do on this arbitrary queue if that method is not thread safe, so it's probably a good idea to dispatch to a known queue right away anyway.

You also seem to not be handling the success BOOL or NSError passed into the completion handler after performChanges. It would be good to only do the follow-on work with placeholder in the case that the changes were successful.

app crashed when creating album
 
 
Q