MCDownloadOperation.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // MCDownloadOperation.h
  3. // MCDownloadManager
  4. //
  5. // Created by M.C on 17/4/6. (QQ:714080794 Gmail:chaoma0609@gmail.com)
  6. // Copyright © 2017年 qikeyun. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import <Foundation/Foundation.h>
  27. #import "MCDownloader.h"
  28. extern NSString * _Nonnull const MCDownloadStartNotification;
  29. extern NSString * _Nonnull const MCDownloadReceiveResponseNotification;
  30. extern NSString * _Nonnull const MCDownloadStopNotification;
  31. extern NSString * _Nonnull const MCDownloadFinishNotification;
  32. /**
  33. Describes a downloader operation. If one wants to use a custom downloader op, it needs to inherit from `NSOperation` and conform to this protocol
  34. */
  35. @protocol MCDownloaderOperationInterface<NSObject>
  36. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  37. inSession:(nullable NSURLSession *)session;
  38. - (nullable id)addHandlersForProgress:(nullable MCDownloaderProgressBlock)progressBlock
  39. completed:(nullable MCDownloaderCompletedBlock)completedBlock;
  40. @end
  41. @interface MCDownloadOperation : NSOperation <MCDownloaderOperationInterface, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
  42. /**
  43. * The request used by the operation's task.
  44. */
  45. @property (strong, nonatomic, nullable) NSURLRequest *request;
  46. /**
  47. * The operation's task
  48. */
  49. @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask;
  50. /**
  51. * The expected size of data.
  52. */
  53. @property (assign, nonatomic) NSInteger expectedSize;
  54. /**
  55. * The response returned by the operation's connection.
  56. */
  57. @property (strong, nonatomic, nullable) NSURLResponse *response;
  58. /**
  59. * Initializes a `MCDownloadOperation` object
  60. *
  61. * @see MCDownloadOperation
  62. *
  63. * @param request the receipt
  64. * @param session the URL session in which this operation will run
  65. *
  66. * @return the initialized instance
  67. */
  68. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  69. inSession:(nullable NSURLSession *)session NS_DESIGNATED_INITIALIZER;
  70. /**
  71. * Adds handlers for progress and completion. Returns a tokent that can be passed to -cancel: to cancel this set of
  72. * callbacks.
  73. *
  74. * @param progressBlock the block executed when a new chunk of data arrives.
  75. * @note the progress block is executed on a background queue
  76. * @param completedBlock the block executed when the download is done.
  77. * @note the completed block is executed on the main queue for success. If errors are found, there is a chance the block will be executed on a background queue
  78. *
  79. * @return the token to use to cancel this set of handlers
  80. */
  81. - (nullable id)addHandlersForProgress:(nullable MCDownloaderProgressBlock)progressBlock
  82. completed:(nullable MCDownloaderCompletedBlock)completedBlock;
  83. /**
  84. * Cancels a set of callbacks. Once all callbacks are canceled, the operation is cancelled.
  85. *
  86. * @param token the token representing a set of callbacks to cancel
  87. *
  88. * @return YES if the operation was stopped because this was the last token to be canceled. NO otherwise.
  89. */
  90. - (BOOL)cancel:(nullable id)token;
  91. @end