YTKChainRequest.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // YTKChainRequest.h
  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 <Foundation/Foundation.h>
  24. NS_ASSUME_NONNULL_BEGIN
  25. @class YTKChainRequest;
  26. @class YTKBaseRequest;
  27. @protocol YTKRequestAccessory;
  28. /// The YTKChainRequestDelegate protocol defines several optional methods you can use
  29. /// to receive network-related messages. All the delegate methods will be called
  30. /// on the main queue. Note the delegate methods will be called when all the requests
  31. /// of chain request finishes.
  32. @protocol YTKChainRequestDelegate <NSObject>
  33. @optional
  34. /// Tell the delegate that the chain request has finished successfully.
  35. ///
  36. /// @param chainRequest The corresponding chain request.
  37. - (void)chainRequestFinished:(YTKChainRequest *)chainRequest;
  38. /// Tell the delegate that the chain request has failed.
  39. ///
  40. /// @param chainRequest The corresponding chain request.
  41. /// @param request First failed request that causes the whole request to fail.
  42. - (void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest*)request;
  43. @end
  44. typedef void (^YTKChainCallback)(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest);
  45. /// YTKBatchRequest can be used to chain several YTKRequest so that one will only starts after another finishes.
  46. /// Note that when used inside YTKChainRequest, a single YTKRequest will have its own callback and delegate
  47. /// cleared, in favor of the batch request callback.
  48. @interface YTKChainRequest : NSObject
  49. /// All the requests are stored in this array.
  50. - (NSArray<YTKBaseRequest *> *)requestArray;
  51. /// The delegate object of the chain request. Default is nil.
  52. @property (nonatomic, weak, nullable) id<YTKChainRequestDelegate> delegate;
  53. /// This can be used to add several accessories object. Note if you use `addAccessory` to add acceesory
  54. /// this array will be automatically created. Default is nil.
  55. @property (nonatomic, strong, nullable) NSMutableArray<id<YTKRequestAccessory>> *requestAccessories;
  56. /// Convenience method to add request accessory. See also `requestAccessories`.
  57. - (void)addAccessory:(id<YTKRequestAccessory>)accessory;
  58. /// Start the chain request, adding first request in the chain to request queue.
  59. - (void)start;
  60. /// Stop the chain request. Remaining request in chain will be cancelled.
  61. - (void)stop;
  62. /// Add request to request chain.
  63. ///
  64. /// @param request The request to be chained.
  65. /// @param callback The finish callback
  66. - (void)addRequest:(YTKBaseRequest *)request callback:(nullable YTKChainCallback)callback;
  67. @end
  68. NS_ASSUME_NONNULL_END