#import <Foundation/Foundation.h> @protocol DownloaderDelegate <NSObject> -(void) downloadCompleted:(NSData*) data; @end #import <Foundation/Foundation.h> #import "DownloaderDelegate.h" typedef void (^CallBack)(NSData *data); @interface Downloader : NSObject { NSString *path; CallBack callback; } @property (weak) id<DownloaderDelegate> delegate; -(void) downloadFile:(NSString*) filePath; -(void) downloadFile:(NSString*) filePath completion:(void(^)(NSData *data)) block; @end #import "Downloader.h" @implementation Downloader @synthesize delegate; -(void) downloadFile:(NSString*) filePath { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self download:(NSString*) path]; }); } -(void) download:(NSString *) resourcePath { NSURL *url = [NSURL URLWithString:resourcePath]; NSData *data = [NSData dataWithContentsOfURL:url]; if (callback) { callback(data); } else { if ([delegate conformsToProtocol:@protocol(DownloaderDelegate)]) { [delegate downloadCompleted:data]; } } } -(void) downloadFile:(NSString*) filePath completion:(void(^)(NSData *data)) block { callback = block; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self download:(NSString*) path]; }); } @end
System programming, Cluster computing, Writing Operating System, iOS application Development, Advanced C programming, Assembly langauge, C++
Sunday, March 31, 2013
Protocol Usage and asynchronous call implementation
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment