I can't manage to get the responseCompletion type correct, at the for cycle.
Tried to replace it with typedef, but got no luck, only more errors.
@interface ImageManager : NSObject
//typedef void(^responseBlock)(UIImage * _Nullable image);
@property (nonatomic, strong) NSDictionary<NSString *,NSMutableArray<void(^)(UIImage * _Nullable image)> *> *loadingResponses;
@end
-(void)fetchImage:(NSString *)urlString and:(void (^_Nonnull)(UIImage * _Nullable image))completionhHandler {
UIImage *image = [self.cache objectForKey:urlString];
if (image != nil) {
completionhHandler(image);
return;
}
if (self.loadingResponses[urlString] != nil) {
[self.loadingResponses[urlString] addObject:completionhHandler];
return;
} else {
[[self.loadingResponses objectForKey:urlString] setArray:[[NSMutableArray alloc] initWithObjects:completionhHandler, nil]];
}
NSURL *url = [[NSURL alloc] initWithString:urlString];
[[self.session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error != nil) {
UIImage *image = [[UIImage alloc] initWithData:data];
[self.cache setObject:image forKey:urlString];
completionhHandler(image);
NSArray *array = [self.loadingResponses objectForKey:urlString];
for ((void(^)(UIImage * _Nullable image) *responseCompletion) in array) {
responseCompletion(image);
}
}
}] resume];
}