下载管理器
单例
+ (instancetype)sharedDownloadManager {
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
移植下载方法
- (void)downloadWithURL:(NSURL *)url progress:(void (^)(float))progress finised:(void (^)(NSString *, NSError *))finised {
HMDownloadOperation *downloader = [HMDownloadOperation downloadWithURL:url progress:progress finised:finised];
[downloader download];
}
下载缓冲池
缓冲池属性
@property (nonatomic, strong) NSMutableDictionary *downloaderCache;
- (NSMutableDictionary *)downloaderCache {
if (_downloaderCache == nil) {
_downloaderCache = [[NSMutableDictionary alloc] init];
}
return _downloaderCache;
}
修改下载方法
- (void)downloadWithURL:(NSURL *)url progress:(void (^)(float))progress finised:(void (^)(NSString *, NSError *))finised {
if (self.downloaderCache[url]) {
NSLog(@"正在玩命下载中...");
return;
}
HMDownloadOperation *downloader = [HMDownloadOperation downloadWithURL:url progress:progress finised:finised];
[self.downloaderCache setObject:downloader forKey:url];
[downloader download];
}
下载完成后,将操作从缓冲池中删除
HMDownloadOperation *downloader = [HMDownloadOperation downloadWithURL:url progress:progress finised:^(NSString *filePath, NSError *error) {
[self.downloaderCache removeObjectForKey:url];
finised(filePath, error);
}];