From edefa3350eff8cde237ff69e85b6e138d08e01bb Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Fri, 31 Mar 2017 13:05:35 +0200 Subject: [PATCH] Use NSURLSession. --- Classes/MetadataImporters/SBChapterDB.m | 2 +- Classes/MetadataImporters/SBMetadataHelper.h | 2 +- Classes/MetadataImporters/SBMetadataHelper.m | 62 +++++++++----------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/Classes/MetadataImporters/SBChapterDB.m b/Classes/MetadataImporters/SBChapterDB.m index b0e159e..c97e6da 100644 --- a/Classes/MetadataImporters/SBChapterDB.m +++ b/Classes/MetadataImporters/SBChapterDB.m @@ -27,7 +27,7 @@ NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.chapterdb.org/chapters/search?title=%@", [SBMetadataHelper urlEncoded:title]]]; NSDictionary *headerOptions = @{@"ApiKey" : API_KEY}; - self.task = [SBMetadataHelper sessionTaskFromUrl:url withHTTPMethod:@"GET" headerOptions:headerOptions completionHandler:^(NSData * _Nullable data) { + self.task = [SBMetadataHelper sessionTaskFromUrl:url HTTPMethod:@"GET" headerOptions:headerOptions cachePolicy:NSURLRequestUseProtocolCachePolicy completionHandler:^(NSData * _Nullable data) { NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:data options:0 error:NULL]; diff --git a/Classes/MetadataImporters/SBMetadataHelper.h b/Classes/MetadataImporters/SBMetadataHelper.h index d6d27b8..6275d7e 100644 --- a/Classes/MetadataImporters/SBMetadataHelper.h +++ b/Classes/MetadataImporters/SBMetadataHelper.h @@ -23,7 +23,7 @@ typedef NS_ENUM(NSUInteger, SBCachePolicy) { + (NSString *)urlEncoded:(NSString *)string; + (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy; -+ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header completionHandler:(void(^)(NSData * _Nullable data))completionHandler; ++ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url HTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header cachePolicy:(NSURLRequestCachePolicy)cachePolicy completionHandler:(void(^)(NSData * _Nullable data))completionHandler; @end diff --git a/Classes/MetadataImporters/SBMetadataHelper.m b/Classes/MetadataImporters/SBMetadataHelper.m index fdafc3c..6ec56b7 100644 --- a/Classes/MetadataImporters/SBMetadataHelper.m +++ b/Classes/MetadataImporters/SBMetadataHelper.m @@ -116,7 +116,6 @@ } } - return results; } @@ -146,46 +145,41 @@ return [NSString stringWithString:r]; } -+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy error:(NSError * __autoreleasing *)error { - NSFileManager *fileManager = [NSFileManager defaultManager]; - NSString *bundleName = [NSBundle mainBundle].infoDictionary[@"CFBundleIdentifier"]; - NSURL *cacheURL = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].firstObject URLByAppendingPathComponent:bundleName]; ++ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy { + dispatch_semaphore_t sem = dispatch_semaphore_create(0); + __block NSData *downloadedData; - - NSURL *fileURL = [cacheURL URLByAppendingPathComponent:[SBMetadataHelper sha256String:url.absoluteString]]; - fileURL = [fileURL URLByAppendingPathExtension:url.pathExtension]; - - if (policy != SBReloadIgnoringLocalCacheData) { - NSDictionary *attrs = [fileURL resourceValuesForKeys:@[NSURLCreationDateKey] error:NULL]; - NSDate *creationDate = attrs[NSURLCreationDateKey]; - - if (creationDate) { - NSTimeInterval oldness = creationDate.timeIntervalSinceNow; - - // if less than 2 hours old or jpg - if ([url.pathExtension caseInsensitiveCompare:@"jpg"] == NSOrderedSame || - (oldness > -60 * 60 * 2) || - policy == SBReturnCacheElseLoad) { - return [NSData dataWithContentsOfURL:fileURL options:0 error:error]; - } - } + NSURLRequestCachePolicy cachePolicy; + switch (policy) { + case SBReturnCacheElseLoad: + cachePolicy = NSURLRequestReturnCacheDataElseLoad; + break; + case SBReloadIgnoringLocalCacheData: + cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; + break; + case SBDefaultPolicy: + default: + cachePolicy = NSURLRequestUseProtocolCachePolicy; + break; } - - NSData *downloadedData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:error]; - [downloadedData writeToURL:fileURL atomically:YES]; - + + NSURLSessionTask *task = [self sessionTaskFromUrl:url HTTPMethod:@"GET" headerOptions:nil cachePolicy:cachePolicy completionHandler:^(NSData * _Nullable data) { + downloadedData = data; + dispatch_semaphore_signal(sem); + }]; + + [task resume]; + + dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); + return downloadedData; } -+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy { - return [SBMetadataHelper downloadDataFromURL:url withCachePolicy:policy error:NULL]; -} - #pragma mark NSURLRequest -+ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header completionHandler:(void(^)(NSData * _Nullable data))completionHandler ++ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url HTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header cachePolicy:(NSURLRequestCachePolicy)cachePolicy completionHandler:(void(^)(NSData * _Nullable data))completionHandler { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url - cachePolicy:NSURLRequestUseProtocolCachePolicy + cachePolicy:cachePolicy timeoutInterval:30.0]; request.HTTPMethod = method; @@ -210,8 +204,8 @@ completionHandler(nil); } } - }]; + return task; }