Last active
December 6, 2016 21:35
-
-
Save petermolnar-dev/c4aff51a762e3946721af590d916af06 to your computer and use it in GitHub Desktop.
PMODownloader.m - With KVO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PMODownloader.h" | |
#import "PMODownloadNotifications.h" | |
@implementation PMODownloader | |
#pragma mark - Public API | |
- (void)downloadDataFromURL:(NSURL *)url { | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
NSURLSession *session = [NSURLSession sharedSession]; | |
NSURLSessionDataTask *task = [session dataTaskWithRequest:request | |
completionHandler: | |
^(NSData *data, NSURLResponse *response, NSError *error) { | |
if (error) { | |
[self notifyObserverDownloadFailure]; | |
} else { | |
//2 | |
self.downloadedData = data; | |
} | |
}]; | |
[task resume]; | |
} | |
//3 | |
#pragma mark - Accessors | |
- (void)setDownloadedData:(NSData *)downloadedData { | |
[self willChangeValueForKey:@"downloadedData"]; | |
if (!_downloadedData) { | |
_downloadedData = [[NSData alloc] init]; | |
} | |
_downloadedData = downloadedData; | |
[self didChangeValueForKey:@"downloadedData"]; | |
} | |
#pragma mark - Notifications | |
- (void)notifyObserverDownloadFailure { | |
[[NSNotificationCenter defaultCenter] postNotificationName:PMODownloadFailed | |
object:self | |
userInfo:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment