|
@@ -11,6 +11,13 @@
|
|
|
#import "RAUtils.h"
|
|
#import "RAUtils.h"
|
|
|
#import "UploadSettingController.h"
|
|
#import "UploadSettingController.h"
|
|
|
|
|
|
|
|
|
|
+@interface RAUploadManager ()
|
|
|
|
|
+
|
|
|
|
|
+@property (nonatomic,strong) NSTimer *retryTimer;
|
|
|
|
|
+@property (nonatomic,strong) NSMutableArray<RAUploadOperation *> *retryArr;
|
|
|
|
|
+
|
|
|
|
|
+@end
|
|
|
|
|
+
|
|
|
@implementation RAUploadManager
|
|
@implementation RAUploadManager
|
|
|
|
|
|
|
|
|
|
|
|
@@ -118,9 +125,11 @@
|
|
|
-(void) dealloc
|
|
-(void) dealloc
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
|
|
+ [self destructRetryTimer];
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
|
|
|
[self.operation_queue removeObserver:self forKeyPath:@"operationCount"];
|
|
[self.operation_queue removeObserver:self forKeyPath:@"operationCount"];
|
|
|
[self.operation_queue removeObserver:self forKeyPath:@"arr_queue"];
|
|
[self.operation_queue removeObserver:self forKeyPath:@"arr_queue"];
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -130,7 +139,9 @@
|
|
|
if([keyPath isEqualToString:@"operationCount"])
|
|
if([keyPath isEqualToString:@"operationCount"])
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
- if([[change objectForKey:NSKeyValueChangeNewKey] intValue]==0)
|
|
|
|
|
|
|
+// [self checkRetryOperations];
|
|
|
|
|
+
|
|
|
|
|
+ if(self.operation_queue.operationCount == 0 && self.retryArr.count == 0)
|
|
|
{
|
|
{
|
|
|
for(NSMutableDictionary* task in self.arr_queue)
|
|
for(NSMutableDictionary* task in self.arr_queue)
|
|
|
{
|
|
{
|
|
@@ -238,15 +249,17 @@
|
|
|
-(void) removeTasks:(NSMutableArray*) tasks
|
|
-(void) removeTasks:(NSMutableArray*) tasks
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
- NSFileManager* fmanager = [NSFileManager new];
|
|
|
|
|
|
|
+
|
|
|
for(NSMutableDictionary* task in tasks)
|
|
for(NSMutableDictionary* task in tasks)
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
[self stopTask:task];
|
|
[self stopTask:task];
|
|
|
- NSError* error = nil;
|
|
|
|
|
- bool bdel=[fmanager removeItemAtPath:[self filePath:task] error:&error];
|
|
|
|
|
- if(!bdel)
|
|
|
|
|
- DebugLog(@"file delete failed path:%@",[self filePath:task]);
|
|
|
|
|
|
|
+ // 文件上传成功才移除文件
|
|
|
|
|
+// NSFileManager* fmanager = [NSFileManager new];
|
|
|
|
|
+// NSError* error = nil;
|
|
|
|
|
+// bool bdel=[fmanager removeItemAtPath:[self filePath:task] error:&error];
|
|
|
|
|
+// if(!bdel)
|
|
|
|
|
+// DebugLog(@"file delete failed path:%@",[self filePath:task]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -285,13 +298,17 @@
|
|
|
|
|
|
|
|
-(void) removeTask:(NSMutableDictionary*) task
|
|
-(void) removeTask:(NSMutableDictionary*) task
|
|
|
{
|
|
{
|
|
|
- NSFileManager* fmanager = [NSFileManager new];
|
|
|
|
|
|
|
+
|
|
|
// [self.arr_queue addObject:task];
|
|
// [self.arr_queue addObject:task];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
[self stopTask:task];
|
|
[self stopTask:task];
|
|
|
- NSError* error = nil;
|
|
|
|
|
- bool bdel=[fmanager removeItemAtPath:[self filePath:task] error:&error];
|
|
|
|
|
- if(!bdel)
|
|
|
|
|
- DebugLog(@"file delete failed path:%@",[self filePath:task]);
|
|
|
|
|
|
|
+ // 上传成功才删除文件
|
|
|
|
|
+// NSFileManager* fmanager = [NSFileManager new];
|
|
|
|
|
+// NSError* error = nil;
|
|
|
|
|
+// bool bdel=[fmanager removeItemAtPath:[self filePath:task] error:&error];
|
|
|
|
|
+// if(!bdel)
|
|
|
|
|
+// DebugLog(@"file delete failed path:%@",[self filePath:task]);
|
|
|
|
|
|
|
|
|
|
|
|
|
// [self.arr_queue_lock lock];
|
|
// [self.arr_queue_lock lock];
|
|
@@ -461,4 +478,86 @@
|
|
|
return YES;
|
|
return YES;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+#pragma mark - Retry
|
|
|
|
|
+
|
|
|
|
|
+- (NSMutableArray<RAUploadOperation *> *)retryArr {
|
|
|
|
|
+ if (!_retryArr) {
|
|
|
|
|
+ _retryArr = [NSMutableArray array];
|
|
|
|
|
+ }
|
|
|
|
|
+ return _retryArr;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)addRetryOperation:(RAUploadOperation *)operation {
|
|
|
|
|
+ if (self.retryTimer == nil) {
|
|
|
|
|
+// self.retryTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(checkRetryOperations) userInfo:nil repeats:YES];
|
|
|
|
|
+ self.retryTimer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(checkRetryOperations) userInfo:nil repeats:YES];
|
|
|
|
|
+ [[NSRunLoop mainRunLoop] addTimer:self.retryTimer forMode:NSRunLoopCommonModes];
|
|
|
|
|
+ }
|
|
|
|
|
+ [self.retryArr addObject:operation];
|
|
|
|
|
+
|
|
|
|
|
+ // 升序
|
|
|
|
|
+ [self.retryArr sortUsingComparator:^NSComparisonResult(RAUploadOperation* _Nonnull operation1, RAUploadOperation* _Nonnull opertation2) {
|
|
|
|
|
+
|
|
|
|
|
+ if (operation1.theRestOfWaitTimeInterval < opertation2.theRestOfWaitTimeInterval) {
|
|
|
|
|
+ return NSOrderedAscending;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return NSOrderedDescending;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)checkRetryOperations {
|
|
|
|
|
+
|
|
|
|
|
+ // 检查是否有需要重试的任务
|
|
|
|
|
+ if (self.retryArr.count == 0) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ NSMutableArray<RAUploadOperation *> *nextStartOperations = [NSMutableArray array];
|
|
|
|
|
+// // 检查当前上传队列等待数量是否小于最大并发数
|
|
|
|
|
+// if (self.operation_queue.operationCount < self.maxThread) {
|
|
|
|
|
+//
|
|
|
|
|
+//// // 根据最大并发数将剩余Operation全部添加进去
|
|
|
|
|
+//// NSInteger minCount = MIN(self.retryArr.count, self.maxThread - self.operation_queue.operationCount);
|
|
|
|
|
+// for (int i = 0; i < self.retryArr.count; i++) {
|
|
|
|
|
+// RAUploadOperation *operation = [self.retryArr objectAtIndex:i];
|
|
|
|
|
+// operation.queuePriority = NSOperationQueuePriorityVeryHigh;
|
|
|
|
|
+// [self.operation_queue addOperation:operation];
|
|
|
|
|
+// [nextStartOperations addObject:operation];
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// } else {
|
|
|
|
|
+ // 调度等待时间到达的Operation
|
|
|
|
|
+ __weak typeof(self) weakSelf = self;
|
|
|
|
|
+
|
|
|
|
|
+ [self.retryArr enumerateObjectsUsingBlock:^(RAUploadOperation * _Nonnull operation, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
|
|
+
|
|
|
|
|
+ if (operation.theRestOfWaitTimeInterval <= 0) {
|
|
|
|
|
+ operation.queuePriority = NSOperationQueuePriorityVeryHigh;
|
|
|
|
|
+ [weakSelf.operation_queue addOperation:operation];
|
|
|
|
|
+ [nextStartOperations addObject:operation];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }];
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ [self.retryArr removeObjectsInArray:nextStartOperations];
|
|
|
|
|
+
|
|
|
|
|
+// if (self.retryArr.count == 0) {
|
|
|
|
|
+// [self destructRetryTimer];
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)destructRetryTimer {
|
|
|
|
|
+ if (self.retryTimer != nil) {
|
|
|
|
|
+ [self.retryTimer setFireDate:[NSDate distantFuture]];
|
|
|
|
|
+ [self.retryTimer invalidate];
|
|
|
|
|
+ self.retryTimer = nil;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
@end
|
|
@end
|