Created
February 22, 2015 13:29
-
-
Save flexaddicted/dc69b08dc7f990c9ed8c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // NetworkRequest.h | |
| typedef void(^NetworkRequestCompletionHandler)(NSData *data); | |
| @interface NetworkRequest : NSObject | |
| @property (nonatomic, strong, readonly) NSURL* url; | |
| - (id)initWithURL:(NSURL*)url; | |
| - (void)performWithCompletionHandler:(NetworkRequestCompletionHandler)completionHandler | |
| @end | |
| // NetworkRequest.m | |
| #import "NetworkRequest.h" | |
| @interface NetworkRequest () | |
| @property (nonatomic, strong, readwrite) NSURL *url; | |
| @property (nonatomic, copy) NetworkRequestCompletionHandler completionHandler; | |
| @property (nonatomic, strong) NetworkRequest *selfRef; | |
| @property (nonatomic, strong) NSData *downloadedData; | |
| @end | |
| @implementation NetworkRequest | |
| - (void)dealloc { | |
| NSLog(@"Hey!! NetworkRequest deallocated"); | |
| } | |
| - (id)initWithURL:(NSURL*)url { | |
| if(self = [super init]) { | |
| _url = url; | |
| } | |
| return self; | |
| } | |
| - (void)performWithCompletionHandler:(NetworkRequestCompletionHandler)completionHandler { | |
| self.completionHandler = completionHandler; | |
| // self.selfRef = self; | |
| __weak NetworkRequest* weakSelf = self; | |
| dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | |
| [weakSelf internal_requestCompleted]; | |
| }); | |
| } | |
| - (void)internal_requestCompleted { | |
| NSLog(@"Hey!! NetworkRequest completed"); | |
| if(self.completionHandler) { | |
| self.completionHandler(self.downloadedData); | |
| } | |
| self.completionHandler = nil; | |
| // self.selfRef = nil; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment