Files
PINRemoteImage/Pod/Classes/PINURLSessionManager.m
Garrett Moon 9d2bd31f95 1.0
2015-07-22 17:29:31 -07:00

82 lines
2.4 KiB
Objective-C

//
// PINURLSessionManager.m
// Pods
//
// Created by Garrett Moon on 6/26/15.
//
//
#import "PINURLSessionManager.h"
@interface PINURLSessionManager () <NSURLSessionDelegate, NSURLSessionDataDelegate>
@property (nonatomic, strong) NSLock *sessionManagerLock;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic, strong) NSMutableDictionary *completions;
@end
@implementation PINURLSessionManager
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration
{
if (self = [super init]) {
self.sessionManagerLock = [[NSLock alloc] init];
self.operationQueue = [[NSOperationQueue alloc] init];
[self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:self.operationQueue];
self.completions = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)invalidateSessionAndCancelTasks
{
[self lock];
[self.session invalidateAndCancel];
[self unlock];
}
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *response, NSError *error))completionHandler
{
[self lock];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:request];
if (completionHandler) {
[self.completions setObject:completionHandler forKey:@(dataTask.taskIdentifier)];
}
[self unlock];
return dataTask;
}
- (void)lock
{
[self.sessionManagerLock lock];
}
- (void)unlock
{
[self.sessionManagerLock unlock];
}
#pragma mark NSURLSessionDataDelegate
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[self.delegate didReceiveData:data forTask:dataTask];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
[self.delegate didCompleteTask:task withError:error];
[self lock];
void (^completionHandler)(NSURLResponse *, NSError *) = self.completions[@(task.taskIdentifier)];
[self unlock];
if (completionHandler) {
completionHandler(task.response, error);
[self.completions removeObjectForKey:@(task.taskIdentifier)];
}
}
@end