RAUploadOperation.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. @interface RAUploadOperation ()
  13. {
  14. // NSString* _threadName;
  15. // NSString* _url;
  16. BOOL executing;
  17. BOOL finished;
  18. NSMutableDictionary* _taskinfo;
  19. int _maxRetry;
  20. }
  21. @end
  22. @implementation RAUploadOperation
  23. - (instancetype)initWithTaskinfo:(NSMutableDictionary*)taskinfo retry:(int) maxRetry
  24. {
  25. self = [super init];
  26. if (self) {
  27. // if (name!=nil)
  28. // _threadName = name;
  29. _maxRetry = maxRetry;
  30. // executing = NO;
  31. // finished = NO;
  32. _taskinfo=taskinfo;
  33. }
  34. return self;
  35. }
  36. - (BOOL)isConcurrent {
  37. return YES;
  38. }
  39. //
  40. - (BOOL)isExecuting {
  41. return executing;
  42. }
  43. - (BOOL)isFinished {
  44. return finished;
  45. }
  46. - (void)start
  47. {
  48. [self willChangeValueForKey:@"isExecuting"];
  49. executing = true;
  50. _taskinfo[@"status"]=[NSNumber numberWithInt:TaskStatusStart];
  51. [self didChangeValueForKey:@"isExecuting"];
  52. NSLog(@"---------------start--------------------");
  53. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  54. // sleep(3);
  55. //
  56. // [self willChangeValueForKey:@"isFinished"];
  57. // finished = true;
  58. // [self didChangeValueForKey:@"isFinished"];
  59. //
  60. //
  61. // return;
  62. __weak typeof(self) weakSelf = self;
  63. __block NSMutableDictionary* block_task = _taskinfo;
  64. [NetworkUtils upload:_taskinfo[@"file_path"] Params:_taskinfo[@"params"] ToHost:_taskinfo[@"url"] Result:^(NSMutableDictionary *result) {
  65. int r=[result[@"result"] intValue];
  66. if(r==2)
  67. {
  68. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusFinish];
  69. }
  70. else
  71. {
  72. if([block_task[@"retry"] intValue]>=_maxRetry)
  73. {
  74. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
  75. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  76. }
  77. else
  78. {
  79. block_task[@"retry"]=[NSNumber numberWithInt:[block_task[@"retry"] intValue]+1];
  80. block_task[@"status"]=[NSNumber numberWithInt:TaskStatusWait];
  81. block_task[@"progress"]=[NSNumber numberWithDouble:0.0];
  82. RAUploadOperation* operation = [[RAUploadOperation alloc] initWithTaskinfo:block_task retry:_maxRetry];
  83. operation.updateUI= self.updateUI;
  84. block_task[@"operation"] = operation;
  85. [Appdelegate.uploadManager.operation_queue addOperation:operation];
  86. // [self upload:url];
  87. }
  88. }
  89. dispatch_async(dispatch_get_main_queue(), ^{
  90. if(self.updateUI)
  91. self.updateUI();
  92. });
  93. [weakSelf willChangeValueForKey:@"isFinished"];
  94. finished = true;
  95. [weakSelf didChangeValueForKey:@"isFinished"];
  96. // [self completeOperation];
  97. } Progress:^(double progress) {
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. // ((UIProgressView*) block_task[@"ui_pregress"]).progress = progress;
  100. block_task[@"progress"]=[NSNumber numberWithFloat:progress];
  101. if(self.updateUI)
  102. self.updateUI();
  103. });
  104. } DecryptHandler:^id(NSString *result) {
  105. return @"bibibi";
  106. }];
  107. //完成下载
  108. // [self didChangeValueForKey:@"isExecuting"];
  109. }
  110. //- (void)completeOperation {
  111. //// [self willChangeValueForKey:@"isFinished"];
  112. //// [self willChangeValueForKey:@"isExecuting"];
  113. ////
  114. //// executing = NO;
  115. //// finished = YES;
  116. ////
  117. //// [self didChangeValueForKey:@"isExecuting"];
  118. //// [self didChangeValueForKey:@"isFinished"];
  119. //}
  120. - (void)dealloc
  121. {
  122. // dumpThreads(@"dealloc");
  123. }
  124. @end