8

My application has function to downloads many photo and video files in tmp folder and save them in camera roll with PHPhotoLibrary API.

The problem is that sometimes (the probability is around 10%) exception happens in the saving process.

The error message in console is,

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This method can only be called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary performChangesAndWait:error:]'

My code is like below:

- (void)saveVideoFileInCameraRoll:(NSString *)videoFilePath
{
    NSURL *videoFileUrl = [NSURL fileURLWithPath:videoFilePath];

    photoLibrarySaveImageCompletion completion = ^(BOOL success, NSError *error) {
        NSLog(@"success=%@, error=%@", (success ? @"YES" : @"NO"), error);
    };

    NSLog(@"videoFileUrl=%@", videoFileUrl);

    [self saveVideoFile:videoFileUrl completion:completion];
}

- (void)saveVideoFile:(NSURL *)fileURL completion:(photoLibrarySaveImageCompletion)completion
{
    NSLog(@"fileURL=%@", fileURL);

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        NSLog(@"fileURL=%@", fileURL);

        // Exception happens on this line as [SIGABRT]
        PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:fileURL]; 

        if (_assetCollection) {
            PHAssetCollectionChangeRequest *assetCollectionChangeRequest =
                [PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
            [assetCollectionChangeRequest addAssets:@[ [assetChangeRequest placeholderForCreatedAsset] ]];
        }
        else {
            NSLog(@"### assetCollection is nil ###");
        }
    }

        completionHandler:^(BOOL success, NSError *_Nullable error) {
            NSLog(@"success=%@, error=%@", (success ? @"YES" : @"NO"), error);

            completion(success, error);
        }];
}

I checked similar case:

Save image to photo library using photo framework

The case crashes every time, but my code rarely crashes.

And unlike the case I'm calling

[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:]

in 'performChanges' block

Also I confirmed the fileURL of video file in tmp folder by NSLog and it's OK outside and inside of 'performChanges' block.

fileURL=file:///private/var/mobile/Containers/Data/Application/C87F0F75-E128-4E9F-AE07-6B914939AC5D/tmp/video3.mp4

I would appreciate it if you would let me know the reason or resolution of this issue.

Community
  • 1
  • 1
M.Masa
  • 532
  • 4
  • 20
  • `This method can only be called from inside of`, which method is the error log talking about? – NSNoob Aug 15 '16 at 09:40
  • @NSNoob The method is `PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:fileURL];` – M.Masa Aug 15 '16 at 10:02
  • have you found the solution? i have the same problem – lithium Dec 01 '16 at 12:41
  • @lithium Not yet. I just avoid this problem by try-catch and retry. But after that, it remains the possibility of other crash issue. – M.Masa Dec 02 '16 at 17:30
  • @M.Masa I am facing the similar issue did u find any solution to this? – reetu Jun 22 '17 at 17:21
  • @reetu I can't find solution yet. Before I asked this issue through Apple's TSI(Technical Support Incident). Finally they asked me to send my source code for you. But I couldn't because the entire code of my application is so huge and complicated, and it's difficult to extract the problem part from entire code. Thank you. – M.Masa Jul 05 '17 at 04:14
  • I've got the error when calling `changeRequestForAsset:` from inside `performChanges:` block, but the call was also inside another asynchronous block. Something to consider. – alex-i Dec 17 '18 at 14:34

0 Answers0