Kjuly / ALAssetsLibrary-CustomPhotoAlbum

A nice ALAssetsLibrary category for saving images & videos into custom photo album.
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
MIT License
405 stars 101 forks source link

Completion handler #27

Closed pronebird closed 9 years ago

pronebird commented 9 years ago

Why both completion and failure blocks take NSError parameters?

[assetsLibrary saveImageData:imageData toAlbum:@"Album" metadata:metadata 
completion:^(NSURL *assetURL, NSError *error) {
    // ...
} failure:^(NSError *error) {
    // ...
}];
Kjuly commented 9 years ago

Hi @pronebird , the errors are for different purposes, as the DOC said:

// This block is executed if the user does not grant access to the caller to access the data managed by the framework or if the data is currently unavailable. typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);

// This block is executed when saving an image by -writeImageToSavedPhotosAlbum:completionBlock: finishes. The assetURL can later be used to reference the saved image. typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL assetURL, NSError error);

Hope it helps :)

pronebird commented 9 years ago

I got it, it's just weird that both handlers accept error, I'd expect one failure and one success block.

Kjuly commented 9 years ago

@pronebird u can wrap it with a success block instead of completion block. Just note that only invoke the success block when the error is nil in completion block.

[assetsLibrary saveImageData:imageData toAlbum:@"Album" metadata:metadata 
completion:^(NSURL *assetURL, NSError *error) {
    if (error) {
      failure(error);
    } else {
      success(assetURL);
    }
} failure:failure];