YTKChainRequest.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // YTKChainRequest.m
  3. //
  4. // Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import "YTKChainRequest.h"
  24. #import "YTKChainRequestAgent.h"
  25. #import "YTKNetworkPrivate.h"
  26. #import "YTKBaseRequest.h"
  27. @interface YTKChainRequest()<YTKRequestDelegate>
  28. @property (strong, nonatomic) NSMutableArray<YTKBaseRequest *> *requestArray;
  29. @property (strong, nonatomic) NSMutableArray<YTKChainCallback> *requestCallbackArray;
  30. @property (assign, nonatomic) NSUInteger nextRequestIndex;
  31. @property (strong, nonatomic) YTKChainCallback emptyCallback;
  32. @end
  33. @implementation YTKChainRequest
  34. - (instancetype)init {
  35. self = [super init];
  36. if (self) {
  37. _nextRequestIndex = 0;
  38. _requestArray = [NSMutableArray array];
  39. _requestCallbackArray = [NSMutableArray array];
  40. _emptyCallback = ^(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest) {
  41. // do nothing
  42. };
  43. }
  44. return self;
  45. }
  46. - (void)start {
  47. if (_nextRequestIndex > 0) {
  48. YTKLog(@"Error! Chain request has already started.");
  49. return;
  50. }
  51. if ([_requestArray count] > 0) {
  52. [self toggleAccessoriesWillStartCallBack];
  53. [self startNextRequest];
  54. [[YTKChainRequestAgent sharedAgent] addChainRequest:self];
  55. } else {
  56. YTKLog(@"Error! Chain request array is empty.");
  57. }
  58. }
  59. - (void)stop {
  60. [self toggleAccessoriesWillStopCallBack];
  61. [self clearRequest];
  62. [[YTKChainRequestAgent sharedAgent] removeChainRequest:self];
  63. [self toggleAccessoriesDidStopCallBack];
  64. }
  65. - (void)addRequest:(YTKBaseRequest *)request callback:(YTKChainCallback)callback {
  66. [_requestArray addObject:request];
  67. if (callback != nil) {
  68. [_requestCallbackArray addObject:callback];
  69. } else {
  70. [_requestCallbackArray addObject:_emptyCallback];
  71. }
  72. }
  73. - (NSArray<YTKBaseRequest *> *)requestArray {
  74. return _requestArray;
  75. }
  76. - (BOOL)startNextRequest {
  77. if (_nextRequestIndex < [_requestArray count]) {
  78. YTKBaseRequest *request = _requestArray[_nextRequestIndex];
  79. _nextRequestIndex++;
  80. request.delegate = self;
  81. [request clearCompletionBlock];
  82. [request start];
  83. return YES;
  84. } else {
  85. return NO;
  86. }
  87. }
  88. #pragma mark - Network Request Delegate
  89. - (void)requestFinished:(YTKBaseRequest *)request {
  90. NSUInteger currentRequestIndex = _nextRequestIndex - 1;
  91. YTKChainCallback callback = _requestCallbackArray[currentRequestIndex];
  92. callback(self, request);
  93. if (![self startNextRequest]) {
  94. [self toggleAccessoriesWillStopCallBack];
  95. if ([_delegate respondsToSelector:@selector(chainRequestFinished:)]) {
  96. [_delegate chainRequestFinished:self];
  97. [[YTKChainRequestAgent sharedAgent] removeChainRequest:self];
  98. }
  99. [self toggleAccessoriesDidStopCallBack];
  100. }
  101. }
  102. - (void)requestFailed:(YTKBaseRequest *)request {
  103. [self toggleAccessoriesWillStopCallBack];
  104. if ([_delegate respondsToSelector:@selector(chainRequestFailed:failedBaseRequest:)]) {
  105. [_delegate chainRequestFailed:self failedBaseRequest:request];
  106. [[YTKChainRequestAgent sharedAgent] removeChainRequest:self];
  107. }
  108. [self toggleAccessoriesDidStopCallBack];
  109. }
  110. - (void)clearRequest {
  111. NSUInteger currentRequestIndex = _nextRequestIndex - 1;
  112. if (currentRequestIndex < [_requestArray count]) {
  113. YTKBaseRequest *request = _requestArray[currentRequestIndex];
  114. [request stop];
  115. }
  116. [_requestArray removeAllObjects];
  117. [_requestCallbackArray removeAllObjects];
  118. }
  119. #pragma mark - Request Accessoies
  120. - (void)addAccessory:(id<YTKRequestAccessory>)accessory {
  121. if (!self.requestAccessories) {
  122. self.requestAccessories = [NSMutableArray array];
  123. }
  124. [self.requestAccessories addObject:accessory];
  125. }
  126. @end