RAUploadOperation.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // RAUploadOperation.m
  3. // test_autolayout
  4. //
  5. // Created by Ray on 03/05/2017.
  6. // Copyright © 2017 USAI. All rights reserved.
  7. //
  8. #import "RAUploadOperation.h"
  9. #import "NetworkUtils.h"
  10. #import <UIKit/UIKit.h>
  11. #import "AppDelegate.h"
  12. #import "RAUtils.h"
  13. @interface RAUploadOperation ()
  14. {
  15. // NSString* _threadName;
  16. // NSString* _url;
  17. BOOL executing;
  18. BOOL finished;
  19. NSMutableDictionary* _taskinfo;
  20. int _maxRetry;
  21. }
  22. @end
  23. @implementation RAUploadOperation
  24. - (instancetype)initWithTaskinfo:(NSMutableDictionary*)taskinfo retry:(int) maxRetry
  25. {
  26. self = [super init];
  27. if (self) {
  28. // if (name!=nil)
  29. // _threadName = name;
  30. _maxRetry = maxRetry;
  31. // executing = NO;
  32. // finished = NO;
  33. _taskinfo=taskinfo;
  34. }
  35. return self;
  36. }
  37. - (BOOL)isConcurrent {
  38. return YES;
  39. }
  40. //
  41. - (BOOL)isExecuting {
  42. return executing;
  43. }
  44. - (BOOL)isFinished {
  45. return finished;
  46. }
  47. - (void)start
  48. {
  49. [self willChangeValueForKey:@"isExecuting"];
  50. executing = true;
  51. _taskinfo[@"status"]=[NSNumber numberWithInt:TaskStatusStart];
  52. [self didChangeValueForKey:@"isExecuting"];
  53. NSLog(@"---------------start--------------------");
  54. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  55. // sleep(3);
  56. //
  57. // [self willChangeValueForKey:@"isFinished"];
  58. // finished = true;
  59. // [self didChangeValueForKey:@"isFinished"];
  60. //
  61. //
  62. // return;
  63. __weak typeof(self) weakSelf = self;
  64. __block NSMutableDictionary* block_task = _taskinfo;
  65. // NSString *md5 = [RAUtils md5WithFile:[self filePath:_taskinfo ]];
  66. NSString* file_path = [self filePath:_taskinfo];
  67. if(file_path==nil)
  68. {
  69. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  70. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  71. [self willChangeValueForKey:@"isFinished"];
  72. finished = true;
  73. [self didChangeValueForKey:@"isFinished"];
  74. return;
  75. }
  76. [NetworkUtils upload:[self filePath:_taskinfo ] Params:_taskinfo[@"params"] ToHost:_taskinfo[@"url"] Result:^(NSMutableDictionary *result) {
  77. DebugLog(@"upload result: %@",result);
  78. int r=[result[@"result"] intValue];
  79. if(r==2)
  80. {
  81. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusFinish];
  82. }
  83. else
  84. {
  85. if([block_task[@"retry"] intValue]>=_maxRetry)
  86. {
  87. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  88. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  89. }
  90. else
  91. {
  92. block_task[@"retry"]=[NSNumber numberWithInt:[block_task[@"retry"] intValue]+1];
  93. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusWait];
  94. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  95. RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:block_task retry:_maxRetry];
  96. operation.updateUI= self.updateUI;
  97. block_task[@"operation"] = operation;
  98. [Appdelegate.uploadManager.operation_queue addOperation:operation];
  99. // [self upload:url];
  100. }
  101. }
  102. dispatch_async(dispatch_get_main_queue(), ^{
  103. if(self.updateUI)
  104. self.updateUI();
  105. });
  106. [weakSelf willChangeValueForKey:@"isFinished"];
  107. finished = true;
  108. [weakSelf didChangeValueForKey:@"isFinished"];
  109. // [self completeOperation];
  110. } Progress:^(double progress) {
  111. dispatch_async(dispatch_get_main_queue(), ^{
  112. // ((UIProgressView*) block_task[@"ui_pregress"]).progress = progress;
  113. block_task[@"progress"]=[NSNumber numberWithFloat:progress];
  114. if(self.updateUI)
  115. self.updateUI();
  116. });
  117. } DecryptHandler:^id(NSString *result) {
  118. return @"bibibi";
  119. }];
  120. //完成下载
  121. // [self didChangeValueForKey:@"isExecuting"];
  122. }
  123. -(NSString*)filePath:(NSMutableDictionary*)task
  124. {
  125. NSString *path = [RAUtils appCacheDirectory];
  126. path= [path stringByAppendingPathComponent:task[@"path"]];
  127. path= [path stringByAppendingPathComponent:task[@"file"]];
  128. NSFileManager* fmanager = [NSFileManager new];
  129. bool file_exist=[fmanager fileExistsAtPath:path];
  130. if(!file_exist)
  131. return nil;
  132. return path;
  133. }
  134. //- (void)completeOperation {
  135. //// [self willChangeValueForKey:@"isFinished"];
  136. //// [self willChangeValueForKey:@"isExecuting"];
  137. ////
  138. //// executing = NO;
  139. //// finished = YES;
  140. ////
  141. //// [self didChangeValueForKey:@"isExecuting"];
  142. //// [self didChangeValueForKey:@"isFinished"];
  143. //}
  144. - (void)dealloc
  145. {
  146. // dumpThreads(@"dealloc");
  147. }
  148. @end