Use NSURLSession.

This commit is contained in:
Damiano Galassi committed 2017-03-31 13:05:35 +02:00
1 parent 6e391ccbff
commit edefa3350e
3 files changed
+30 -36

No files matched your search

+1 -1
View File
@@ -27,7 +27,7 @@
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.chapterdb.org/chapters/search?title=%@", [SBMetadataHelper urlEncoded:title]]]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.chapterdb.org/chapters/search?title=%@", [SBMetadataHelper urlEncoded:title]]];
NSDictionary *headerOptions = @{@"ApiKey" : API_KEY}; 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 NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:data
options:0 options:0
error:NULL]; error:NULL];
+1 -1
View File
@@ -23,7 +23,7 @@ typedef NS_ENUM(NSUInteger, SBCachePolicy) {
+ (NSString *)urlEncoded:(NSString *)string; + (NSString *)urlEncoded:(NSString *)string;
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy; + (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 @end
+28 -34
View File
@@ -116,7 +116,6 @@
} }
} }
return results; return results;
} }
@@ -146,46 +145,41 @@
return [NSString stringWithString:r]; return [NSString stringWithString:r];
} }
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy error:(NSError * __autoreleasing *)error { + (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy {
NSFileManager *fileManager = [NSFileManager defaultManager]; dispatch_semaphore_t sem = dispatch_semaphore_create(0);
NSString *bundleName = [NSBundle mainBundle].infoDictionary[@"CFBundleIdentifier"]; __block NSData *downloadedData;
NSURL *cacheURL = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].firstObject URLByAppendingPathComponent:bundleName];
NSURLRequestCachePolicy cachePolicy;
NSURL *fileURL = [cacheURL URLByAppendingPathComponent:[SBMetadataHelper sha256String:url.absoluteString]]; switch (policy) {
fileURL = [fileURL URLByAppendingPathExtension:url.pathExtension]; case SBReturnCacheElseLoad:
cachePolicy = NSURLRequestReturnCacheDataElseLoad;
if (policy != SBReloadIgnoringLocalCacheData) { break;
NSDictionary<NSString *, id> *attrs = [fileURL resourceValuesForKeys:@[NSURLCreationDateKey] error:NULL]; case SBReloadIgnoringLocalCacheData:
NSDate *creationDate = attrs[NSURLCreationDateKey]; cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
break;
if (creationDate) { case SBDefaultPolicy:
NSTimeInterval oldness = creationDate.timeIntervalSinceNow; default:
cachePolicy = NSURLRequestUseProtocolCachePolicy;
// if less than 2 hours old or jpg break;
if ([url.pathExtension caseInsensitiveCompare:@"jpg"] == NSOrderedSame ||
(oldness > -60 * 60 * 2) ||
policy == SBReturnCacheElseLoad) {
return [NSData dataWithContentsOfURL:fileURL options:0 error:error];
}
}
} }
NSData *downloadedData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:error]; NSURLSessionTask *task = [self sessionTaskFromUrl:url HTTPMethod:@"GET" headerOptions:nil cachePolicy:cachePolicy completionHandler:^(NSData * _Nullable data) {
[downloadedData writeToURL:fileURL atomically:YES]; downloadedData = data;
dispatch_semaphore_signal(sem);
}];
[task resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return downloadedData; return downloadedData;
} }
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy {
return [SBMetadataHelper downloadDataFromURL:url withCachePolicy:policy error:NULL];
}
#pragma mark NSURLRequest #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 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy cachePolicy:cachePolicy
timeoutInterval:30.0]; timeoutInterval:30.0];
request.HTTPMethod = method; request.HTTPMethod = method;
@@ -210,8 +204,8 @@
completionHandler(nil); completionHandler(nil);
} }
} }
}]; }];
return task; return task;
} }