RAUploadOperation.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. [NetworkUtils upload:[self filePath:_taskinfo ] Params:_taskinfo[@"params"] ToHost:_taskinfo[@"url"] Result:^(NSMutableDictionary *result) {
  66. int r=[result[@"result"] intValue];
  67. if(r==2)
  68. {
  69. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusFinish];
  70. }
  71. else
  72. {
  73. if([block_task[@"retry"] intValue]>=_maxRetry)
  74. {
  75. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  76. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  77. }
  78. else
  79. {
  80. block_task[@"retry"]=[NSNumber numberWithInt:[block_task[@"retry"] intValue]+1];
  81. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusWait];
  82. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  83. RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:block_task retry:_maxRetry];
  84. operation.updateUI= self.updateUI;
  85. block_task[@"operation"] = operation;
  86. [Appdelegate.uploadManager.operation_queue addOperation:operation];
  87. // [self upload:url];
  88. }
  89. }
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. if(self.updateUI)
  92. self.updateUI();
  93. });
  94. [weakSelf willChangeValueForKey:@"isFinished"];
  95. finished = true;
  96. [weakSelf didChangeValueForKey:@"isFinished"];
  97. // [self completeOperation];
  98. } Progress:^(double progress) {
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. // ((UIProgressView*) block_task[@"ui_pregress"]).progress = progress;
  101. block_task[@"progress"]=[NSNumber numberWithFloat:progress];
  102. if(self.updateUI)
  103. self.updateUI();
  104. });
  105. } DecryptHandler:^id(NSString *result) {
  106. return @"bibibi";
  107. }];
  108. //完成下载
  109. // [self didChangeValueForKey:@"isExecuting"];
  110. }
  111. -(NSString*)filePath:(NSMutableDictionary*)task
  112. {
  113. NSString *path = [RAUtils appCacheDirectory];
  114. path= [path stringByAppendingPathComponent:task[@"path"]];
  115. path= [path stringByAppendingPathComponent:task[@"file"]];
  116. return path;
  117. }
  118. //- (void)completeOperation {
  119. //// [self willChangeValueForKey:@"isFinished"];
  120. //// [self willChangeValueForKey:@"isExecuting"];
  121. ////
  122. //// executing = NO;
  123. //// finished = YES;
  124. ////
  125. //// [self didChangeValueForKey:@"isExecuting"];
  126. //// [self didChangeValueForKey:@"isFinished"];
  127. //}
  128. - (void)dealloc
  129. {
  130. // dumpThreads(@"dealloc");
  131. }
  132. @end