I am using ALAssetsLibrary to save the images download from online server. Each image is an imageObject with image details. It will be save to the photo album (to save and retrieve the url and the url will be converted to the UUID of the image). Once the UUID is added to the imageObject, the imageObject will be added to REALM database.
The problem is that the code save to photo album is not executed. It was ignored. I hope my question is clear, what should i do to make my code (block) executable?
[self writeImagesSuccessively:[[NSMutableArray alloc] initWithArray:tempOSArray] completion:^(id result) {
NSString *res = (NSString *)result;
if ([res isEqualToString:@"finished"]) {
//Add to realm
} else {
NSLog(@"Unable to finish writing images");
}
}];
The above code will call this method which will call addImageToAssetLibrary to save the photos to the album and return a unique UUID for each individual image.
- (void) writeImagesSuccessively:(NSMutableArray*)imgArr completion:(void(^)(id result))completionHandler {
if ([imgArr count] == 0) {
if (completionHandler) {
// Signal completion of writing all images in imgArr
completionHandler(@"finished");
}
return;
}
ImageObject* imgObj = [imgArr firstObject];
[imgArr removeObjectAtIndex:0];
__block __typeof__(self) _self = self;
[self addImageToAssetLibrary:imgObj completion:^(BOOL success, NSString *response) {
if (success) {
imgObj.imageName = response;
NSLog(@"***SAVED IMAGE: %@, %@, %lu", imgObj.imageName, response, (unsigned long)[imgArr count]);
} else {
NSLog(@"Could not save %@", imgObj.imageName);
}
[_self writeImagesSuccessively:imgArr completion:completionHandler];
}];
}
This method will add the ImageObject from the imgObj Array to the photo album and return the UUID. However, // 1) Create Album
[self.library addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group) {
NSLog(@"added album:%@", albumName);
}
is not being executed.
- (void) addImageToAssetLibrary:(ImageObject *)imgObj completion:(void(^)
(BOOL success, NSString* response))completion
{
NSData *imgData = UIImageJPEGRepresentation(imgObj.image, 0.5);
NSString *albumName = @"AlgoAccess Photo Album";
// What is __block?
// Distinct storage type, where modifications to the var done inside the block are also visible outside of it
__block __typeof__(self) _self = self;
__block ALAssetsGroup* groupToAddTo;
__block ALAssetsLibrary* library;
// 1) Create Album **NOT EXECUTED
[self.library addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group) {
NSLog(@"added album:%@", albumName);
}
} //**NEXT LINE OF EXECUTION IS HERE
EDIT: 1 suspect, my block is not being called and some other block is being called instead. Any1 can suggest how do i go about debugging for such causes?