|
@@ -23,11 +23,22 @@
|
|
|
|
|
|
if (self = [super init]) {
|
|
|
_maxCount = 3;
|
|
|
-
|
|
|
+ [self registeNotification];
|
|
|
}
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
+- (void)dealloc {
|
|
|
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)didResiveDownloadFileCompete:(NSNotification *)noti {
|
|
|
+ mixDownloadOperation *operation = noti.object;
|
|
|
+ if (operation) {
|
|
|
+ [self.operations removeObject:operation];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
- (NSMutableArray *)getOperationDoing {
|
|
|
|
|
|
NSMutableArray *doingArray = [NSMutableArray array];
|
|
@@ -39,6 +50,19 @@
|
|
|
return doingArray;
|
|
|
}
|
|
|
|
|
|
+- (NSMutableArray *)getOperationWaiting {
|
|
|
+
|
|
|
+ NSMutableArray *waitingArray = [NSMutableArray array];
|
|
|
+ [self.operations enumerateObjectsUsingBlock:^(mixDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
+
|
|
|
+ mixDownloadOperation *operation = (mixDownloadOperation *)obj;
|
|
|
+ if (operation.downloadState == DownloadStateWaiting) {
|
|
|
+ [waitingArray addObject:operation];
|
|
|
+ }
|
|
|
+ }];
|
|
|
+ return waitingArray;
|
|
|
+}
|
|
|
+
|
|
|
#pragma mark - handle Out operations
|
|
|
- (void)addDownloadWithSession:(NSURLSession *)session URL:(NSURL *)url begin:(void(^)(NSString *))begin progress:(void(^)(NSInteger,NSInteger))progress complete:(void(^)(NSDictionary *,NSError *))complet {
|
|
|
// 获取operation对象
|
|
@@ -73,6 +97,94 @@
|
|
|
}
|
|
|
|
|
|
|
|
|
+- (void)operateDownloadWithUrl:(NSString *)url session:(NSURLSession *)session handle:(DownloadHandleType)handle {
|
|
|
+ // 1、任务列表里取任务
|
|
|
+ mixDownloadOperation *operation = [self operationWithUrl:url];
|
|
|
+
|
|
|
+ // 2、本地plist文件里提取的任务
|
|
|
+ if (!operation) {
|
|
|
+ NSDictionary *dict = [mixDownloadCacheManager queryFileInfoWithUrl:url];
|
|
|
+ operation = [mixDownloadOperation mj_objectWithKeyValues:dict];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3、本地plist文件里提取的任务不存在dataTask
|
|
|
+ if (!operation.dataTask && operation.currentSize != operation.totalSize) {
|
|
|
+ operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
|
|
|
+ [self.operations addObject:operation];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (operation) {
|
|
|
+
|
|
|
+ switch (handle) {
|
|
|
+ case DownloadHandleTypeStart:
|
|
|
+
|
|
|
+ switch (operation.dataTask.state) {
|
|
|
+ case NSURLSessionTaskStateRunning:
|
|
|
+ HLog(@"NSURLSessionTaskStateRunning");
|
|
|
+ break;
|
|
|
+
|
|
|
+ case NSURLSessionTaskStateSuspended:
|
|
|
+ HLog(@"NSURLSessionTaskStateSuspended");
|
|
|
+ operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
|
|
|
+ break;
|
|
|
+
|
|
|
+ case NSURLSessionTaskStateCanceling:
|
|
|
+ HLog(@"NSURLSessionTaskStateCanceling");
|
|
|
+ operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
|
|
|
+ break;
|
|
|
+
|
|
|
+ case NSURLSessionTaskStateCompleted:
|
|
|
+ HLog(@"NSURLSessionTaskStateCompleted");
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ([self getOperationDoing].count >= self.maxCount) { // 下载中任务数超过最大任务限制
|
|
|
+ HLog(@"下载中的任务数超过最大限制")
|
|
|
+// [operation.dataTask suspend]; // 暂停
|
|
|
+ [self operationWaitingWithOperation:operation];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ [operation.dataTask resume]; // 开始
|
|
|
+ [self operationStartWithOperation:operation];
|
|
|
+ break;
|
|
|
+ case DownloadHandleTypeSuspend:
|
|
|
+ [operation.dataTask suspend]; // 暂停
|
|
|
+ [self operationSuspendWithOperation:operation];
|
|
|
+ break;
|
|
|
+ case DownloadHandleTypeCancel:
|
|
|
+ if (operation.dataTask) { // 任务列表删除任务
|
|
|
+ [operation.dataTask cancel]; // 取消
|
|
|
+ [self.operations removeObject:operation];
|
|
|
+ }
|
|
|
+ [mixDownloadCacheManager deleteFileWithUrl:url]; // plist删除任务
|
|
|
+ [self operationDeleteWithOperation:operation];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)suspendAllTasksWithSession:(NSURLSession *)session {
|
|
|
+ // 暂停所有的任务
|
|
|
+ [self.operations enumerateObjectsUsingBlock:^(mixDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
+
|
|
|
+ mixDownloadOperation *operation = (mixDownloadOperation *)obj;
|
|
|
+ if (operation.currentSize != operation.totalSize) { // 暂停未完成的下载任务
|
|
|
+ if (!operation.dataTask) { // 给plist里的任务添加dataTask 添加到operations
|
|
|
+ operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
|
|
|
+ }
|
|
|
+ [operation.dataTask suspend];
|
|
|
+ [self operationSuspendWithOperation:operation];
|
|
|
+ }else {
|
|
|
+ HLog(@"已完成的任务");
|
|
|
+ }
|
|
|
+
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
- (void)startAllTasksWithSession:(NSURLSession *)session {
|
|
|
|
|
|
// 开始所有的任务
|
|
@@ -118,11 +230,47 @@
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
+#pragma mark-监听通知
|
|
|
+- (void)registeNotification {
|
|
|
+ // 监听完成通知
|
|
|
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didResiveDownloadFileCompete:) name:mixDownloadCompleteNoti object:nil];
|
|
|
+
|
|
|
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:mixDownloadTaskExeEnd object:nil];
|
|
|
+// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeError object:nil];
|
|
|
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:mixDownloadTaskExeSuspend object:nil];
|
|
|
+// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeDelete object:nil];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)taskExeEnd:(NSNotification *)notification
|
|
|
+{
|
|
|
+ NSMutableArray *operationWaiting = [self getOperationWaiting];
|
|
|
+
|
|
|
+ [operationWaiting enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
+ mixDownloadOperation *operation = obj;
|
|
|
+ if ([self getOperationDoing].count < self.maxCount) {
|
|
|
+ [self operateDownloadWithUrl:operation.url session:self.session handle:DownloadHandleTypeStart];
|
|
|
+
|
|
|
+ }else {
|
|
|
+ *stop = YES;
|
|
|
+ }
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark 发送通知
|
|
|
+// 暂停某一个operation 保存本地 通知外界
|
|
|
+- (void)operationSuspendWithOperation:(mixDownloadOperation *)operation {
|
|
|
+ operation.downloadState = DownloadStateSuspended;
|
|
|
+ [mixDownloadCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
|
|
|
+ //没有必要再操作的时候通知外面
|
|
|
+ //[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeSuspend object:nil userInfo:@{@"operation" : operation}];
|
|
|
+}
|
|
|
+
|
|
|
// 重启某一个operation 保存本地 通知外界
|
|
|
- (void)operationStartWithOperation:(mixDownloadOperation *)operation {
|
|
|
operation.downloadState = DownloadStateDoing;
|
|
|
[mixDownloadCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
|
|
|
|
|
|
+ //没有必要再操作的时候通知外面
|
|
|
//[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeing object:nil userInfo:@{@"operation" : operation}];
|
|
|
}
|
|
|
|
|
@@ -131,9 +279,17 @@
|
|
|
operation.downloadState = DownloadStateWaiting;
|
|
|
[mixDownloadCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
|
|
|
|
|
|
+ //没有必要再操作的时候通知外面
|
|
|
//[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeSuspend object:nil userInfo:@{@"operation" : operation}];
|
|
|
}
|
|
|
|
|
|
+// 删除某一个operation 保存本地 通知外界
|
|
|
+- (void)operationDeleteWithOperation:(mixDownloadOperation *)operation {
|
|
|
+ //1. 任务删除了为什么不删除本地数据?
|
|
|
+
|
|
|
+ //[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeDelete object:nil userInfo:nil];
|
|
|
+}
|
|
|
+
|
|
|
#pragma mark - handle download
|
|
|
- (void)dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response {
|
|
|
[[self oprationWithDataTask:dataTask] operateWithResponse:response];
|