Skip to content

Instantly share code, notes, and snippets.

@pm-dev
Last active August 29, 2015 14:00
Show Gist options
  • Save pm-dev/11205565 to your computer and use it in GitHub Desktop.
Save pm-dev/11205565 to your computer and use it in GitHub Desktop.
@interface CBImageStore () <NSURLSessionTaskDelegate>
@property (nonatomic, strong) NSMapTable *imageTaskCompletionsByTaskID;
@property (nonatomic, strong) NSMapTable *imageURLTaskCompletionsByTaskID;
@property (nonatomic, strong) NSURLSession *nsurlsession;
@end
@implementation CBImageStore
+ (CBImageStore *)sharedStore
{
static CBImageStore *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[self alloc] init];
});
return _sharedClient;
}
- (NSURLSession *) nsurlsession
{
if (!_nsurlsession) {
_nsurlsession = // create NSURLSession with self as delegate
}
return _nsurlsession;
}
- (NSMapTable *) imageTaskCompletionsByTaskID
{
if (!_imageTaskCompletionsByTaskID) {
_imageTaskCompletionsByTaskID = [NSMapTable mapTableWithKeyOptions:NSMapTableCopyIn
valueOptions:NSMapTableCopyIn];
}
return _imageTaskCompletionsByTaskID;
}
- (NSMapTable *) imageURLTaskCompletionsByTaskID
{
if (!_imageURLTaskCompletionsByTaskID) {
_imageURLTaskCompletionsByTaskID = [NSMapTable mapTableWithKeyOptions:NSMapTableCopyIn
valueOptions:NSMapTableCopyIn];
}
return _imageURLTaskCompletionsByTaskID;
}
- (void) imageForImageName:(NSString *)imageName completion:(void (^)(UIImage *))completionWithImage
{
[self imageNameToImageURLMapping:^(NSDictionary *mapping) {
NSURL *imageURL = mapping[imageName];
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
NSURLSessionDownloadTask *task = [self.nsurlsession downloadTaskWithRequest:request];
self.imageURLTaskCompletionsByTaskID[@(task.taskIdentifier)] = completionWithMapping;
[task resume];
}];
}
- (void) imageNameToImageURLMapping:(void (^)(NSDicationary *))completionWithMapping
{
NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] objectForKey:@"imageURLMapping"];
if (mapping) {
completionWithMapping(mapping);
}
else {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"connors.api/imageURLs"]];
NSURLSessionDataTask *task = [self.nsurlsession dataTaskWithRequest:request];
self.imageURLTaskCompletionsByTaskID[@(task.taskIdentifier)] = completionWithMapping;
[task resume];
}
}
- (void)URLSession:(__unused NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
void(^completionWithMapping)(NSDictionary *) = [self.imageURLTaskCompletionsByTaskID objectForKey:@(task.taskIdentifier)];
if (completionWithMapping) {
NSDictionary *mapping = [self makeMappingFromResponse:task.response]; // You probably already have this method
[[NSUserDefaults standardUserDefaults] setObject:mapping forKey:@"imageURLMapping"];
completionWithMapping(mapping);
[self.imageURLTaskCompletionsByTaskID removeObjectForKey:@(task.taskIdentifier)];
}
}
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
void(^completionWithImage)(UIImage *) = [self.imageTaskCompletionsByTaskID objectForKey:@(downloadTask.taskIdentifier)];
if (completionWithImage) {
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
// You'd want to save the location of the image somewhere so you don't have to do a network request each time.
completionWithImage(image);
[self.imageTaskCompletionsByTaskID removeObjectForKey:@(downloadTask.taskIdentifier)];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment