|
|
@@ -0,0 +1,154 @@
|
|
|
+//
|
|
|
+// RAUploadManager.m
|
|
|
+// test_autolayout
|
|
|
+//
|
|
|
+// Created by Ray on 02/05/2017.
|
|
|
+// Copyright © 2017 USAI. All rights reserved.
|
|
|
+//
|
|
|
+#import <UIKit/UIKit.h>
|
|
|
+#import "RAUploadManager.h"
|
|
|
+#import "NetworkUtils.h"
|
|
|
+
|
|
|
+@implementation RAUploadManager
|
|
|
+
|
|
|
+- (instancetype)init {
|
|
|
+ if (self = [super init]) {
|
|
|
+
|
|
|
+ self.arr_queue = [[NSMutableArray alloc] init];
|
|
|
+ self.newtaskStatus = TaskStatusWait;
|
|
|
+ self.maxThread = 3;
|
|
|
+ self.removeError=true;
|
|
|
+ self.removeFinish = true;
|
|
|
+
|
|
|
+ self.operation_queue = [NSOperationQueue new];
|
|
|
+ self.operation_queue.maxConcurrentOperationCount = self.maxThread;
|
|
|
+// self.backgroundColor = [UIColor clearColor];
|
|
|
+ }
|
|
|
+ return self;
|
|
|
+}
|
|
|
+//-(void) upload:(NSString*) url
|
|
|
+//{
|
|
|
+// while(self.activeThread<self.maxThread)
|
|
|
+// {
|
|
|
+// bool nomore=false;
|
|
|
+// for(int i=0;i<self.arr_queue.count;i++)
|
|
|
+// {
|
|
|
+// NSMutableDictionary* task = self.arr_queue[i];
|
|
|
+//
|
|
|
+// TaskStatus ts=[task[@"status"] intValue];
|
|
|
+// if (ts==TaskStatusWait) {
|
|
|
+// self.activeThread--;
|
|
|
+// [self uploadTask:task url:url];
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// if(i==self.arr_queue.count-1)
|
|
|
+// nomore=true;
|
|
|
+//
|
|
|
+// }
|
|
|
+// if(nomore)
|
|
|
+// break;
|
|
|
+// }
|
|
|
+//}
|
|
|
+-(void) addTask:(NSMutableDictionary*) task
|
|
|
+{
|
|
|
+
|
|
|
+// [self.arr_queue addObject:task];
|
|
|
+
|
|
|
+
|
|
|
+ if(self.newtaskStatus==TaskStatusWait)
|
|
|
+ {
|
|
|
+
|
|
|
+ [self startTask:task];
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ [[self mutableArrayValueForKey:@"arr_queue"] addObject:task];
|
|
|
+}
|
|
|
+-(void) addTasks:(NSMutableArray*) tasks
|
|
|
+{
|
|
|
+
|
|
|
+ if(self.newtaskStatus==TaskStatusWait)
|
|
|
+ {
|
|
|
+ for(NSMutableDictionary* task in tasks)
|
|
|
+ [self startTask:task];
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ [[self mutableArrayValueForKey:@"arr_queue"] addObjectsFromArray:tasks];
|
|
|
+}
|
|
|
+
|
|
|
+-(void) removeTasks:(NSMutableArray*) tasks
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+ for(NSMutableDictionary* task in tasks)
|
|
|
+ [self stopTask:task];
|
|
|
+ [[self mutableArrayValueForKey:@"arr_queue"] removeObjectsInArray:tasks];
|
|
|
+}
|
|
|
+
|
|
|
+-(void) removeTask:(NSMutableDictionary*) task
|
|
|
+{
|
|
|
+
|
|
|
+ // [self.arr_queue addObject:task];
|
|
|
+ [self stopTask:task];
|
|
|
+ [[self mutableArrayValueForKey:@"arr_queue"] removeObject:task];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+-(void) stopTask:(NSMutableDictionary*) task
|
|
|
+{
|
|
|
+
|
|
|
+ if([task[@"status"] intValue]!=TaskStatusStart&&[task[@"status"] intValue]!=TaskStatusWait)
|
|
|
+ return; // task 已停止,返回
|
|
|
+
|
|
|
+ RAUploadOperation* operation = task[@"operation"];
|
|
|
+ [operation cancel];
|
|
|
+ task[@"status"] = [NSNumber numberWithInt:TaskStatusStop] ;
|
|
|
+ task[@"progress"] = [NSNumber numberWithDouble:0.0];
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+-(void) startTask:(NSMutableDictionary*) task
|
|
|
+{
|
|
|
+ __block NSMutableDictionary* block_task = task;
|
|
|
+ __weak typeof(self) weakSelf = self;
|
|
|
+
|
|
|
+ if([task[@"status"] intValue]==TaskStatusStart||[task[@"status"] intValue]==TaskStatusWait)
|
|
|
+ return; // task 已在执行队列,返回
|
|
|
+
|
|
|
+ RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:task retry:self.maxRetry];
|
|
|
+ [operation setCompletionBlock:^{
|
|
|
+ switch ([block_task[@"status"] intValue]) {
|
|
|
+ case TaskStatusFinish:
|
|
|
+ {
|
|
|
+ if(weakSelf.removeFinish)
|
|
|
+ [self removeTask:block_task];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case TaskStatusError:
|
|
|
+ {
|
|
|
+ if(weakSelf.removeError)
|
|
|
+ [self removeTask:block_task];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }];
|
|
|
+ task[@"operation"] = operation;
|
|
|
+ task[@"status"] = [NSNumber numberWithInt:self.newtaskStatus] ;
|
|
|
+ [self.operation_queue addOperation:operation];
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+//- (void) uploadTask:(NSMutableDictionary*) task url:(NSString*)url{
|
|
|
+//
|
|
|
+//// __weak typeof(self) weakself = self;
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//}
|
|
|
+@end
|