BFTask.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2014, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #import <Foundation/Foundation.h>
  11. #import <Bolts/BFCancellationToken.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /*!
  14. Error domain used if there was multiple errors on <BFTask taskForCompletionOfAllTasks:>.
  15. */
  16. extern NSString *const BFTaskErrorDomain;
  17. /*!
  18. An error code used for <BFTask taskForCompletionOfAllTasks:>, if there were multiple errors.
  19. */
  20. extern NSInteger const kBFMultipleErrorsError;
  21. /*!
  22. An exception that is thrown if there was multiple exceptions on <BFTask taskForCompletionOfAllTasks:>.
  23. @deprecated `BFTask` exception handling is deprecated and will be removed in a future release.
  24. */
  25. extern NSString *const BFTaskMultipleExceptionsException
  26. __attribute__((deprecated("`BFTask` exception handling is deprecated and will be removed in a future release.")));
  27. /*!
  28. An error userInfo key used if there were multiple errors on <BFTask taskForCompletionOfAllTasks:>.
  29. Value type is `NSArray<NSError *> *`.
  30. */
  31. extern NSString *const BFTaskMultipleErrorsUserInfoKey;
  32. /*!
  33. An error userInfo key used if there were multiple exceptions on <BFTask taskForCompletionOfAllTasks:>.
  34. Value type is `NSArray<NSException *> *`.
  35. @deprecated `BFTask` exception handling is deprecated and will be removed in a future release.
  36. */
  37. extern NSString *const BFTaskMultipleExceptionsUserInfoKey
  38. __attribute__((deprecated("`BFTask` exception handling is deprecated and will be removed in a future release.")));
  39. @class BFExecutor;
  40. @class BFTask;
  41. /*!
  42. The consumer view of a Task. A BFTask has methods to
  43. inspect the state of the task, and to add continuations to
  44. be run once the task is complete.
  45. */
  46. @interface BFTask<__covariant ResultType> : NSObject
  47. /*!
  48. A block that can act as a continuation for a task.
  49. */
  50. typedef __nullable id(^BFContinuationBlock)(BFTask<ResultType> *t);
  51. /*!
  52. Creates a task that is already completed with the given result.
  53. @param result The result for the task.
  54. */
  55. + (instancetype)taskWithResult:(nullable ResultType)result;
  56. /*!
  57. Creates a task that is already completed with the given error.
  58. @param error The error for the task.
  59. */
  60. + (instancetype)taskWithError:(NSError *)error;
  61. /*!
  62. Creates a task that is already completed with the given exception.
  63. @param exception The exception for the task.
  64. @deprecated `BFTask` exception handling is deprecated and will be removed in a future release.
  65. */
  66. + (instancetype)taskWithException:(NSException *)exception
  67. __attribute__((deprecated("`BFTask` exception handling is deprecated and will be removed in a future release.")));
  68. /*!
  69. Creates a task that is already cancelled.
  70. */
  71. + (instancetype)cancelledTask;
  72. /*!
  73. Returns a task that will be completed (with result == nil) once
  74. all of the input tasks have completed.
  75. @param tasks An `NSArray` of the tasks to use as an input.
  76. */
  77. + (instancetype)taskForCompletionOfAllTasks:(nullable NSArray<BFTask *> *)tasks;
  78. /*!
  79. Returns a task that will be completed once all of the input tasks have completed.
  80. If all tasks complete successfully without being faulted or cancelled the result will be
  81. an `NSArray` of all task results in the order they were provided.
  82. @param tasks An `NSArray` of the tasks to use as an input.
  83. */
  84. + (instancetype)taskForCompletionOfAllTasksWithResults:(nullable NSArray<BFTask *> *)tasks;
  85. /*!
  86. Returns a task that will be completed once there is at least one successful task.
  87. The first task to successuly complete will set the result, all other tasks results are
  88. ignored.
  89. @param tasks An `NSArray` of the tasks to use as an input.
  90. */
  91. + (instancetype)taskForCompletionOfAnyTask:(nullable NSArray<BFTask *> *)tasks;
  92. /*!
  93. Returns a task that will be completed a certain amount of time in the future.
  94. @param millis The approximate number of milliseconds to wait before the
  95. task will be finished (with result == nil).
  96. */
  97. + (instancetype)taskWithDelay:(int)millis;
  98. /*!
  99. Returns a task that will be completed a certain amount of time in the future.
  100. @param millis The approximate number of milliseconds to wait before the
  101. task will be finished (with result == nil).
  102. @param token The cancellation token (optional).
  103. */
  104. + (instancetype)taskWithDelay:(int)millis cancellationToken:(nullable BFCancellationToken *)token;
  105. /*!
  106. Returns a task that will be completed after the given block completes with
  107. the specified executor.
  108. @param executor A BFExecutor responsible for determining how the
  109. continuation block will be run.
  110. @param block The block to immediately schedule to run with the given executor.
  111. @returns A task that will be completed after block has run.
  112. If block returns a BFTask, then the task returned from
  113. this method will not be completed until that task is completed.
  114. */
  115. + (instancetype)taskFromExecutor:(BFExecutor *)executor withBlock:(nullable id (^)())block;
  116. // Properties that will be set on the task once it is completed.
  117. /*!
  118. The result of a successful task.
  119. */
  120. @property (nullable, nonatomic, strong, readonly) ResultType result;
  121. /*!
  122. The error of a failed task.
  123. */
  124. @property (nullable, nonatomic, strong, readonly) NSError *error;
  125. /*!
  126. The exception of a failed task.
  127. @deprecated `BFTask` exception handling is deprecated and will be removed in a future release.
  128. */
  129. @property (nullable, nonatomic, strong, readonly) NSException *exception
  130. __attribute__((deprecated("`BFTask` exception handling is deprecated and will be removed in a future release.")));
  131. /*!
  132. Whether this task has been cancelled.
  133. */
  134. @property (nonatomic, assign, readonly, getter=isCancelled) BOOL cancelled;
  135. /*!
  136. Whether this task has completed due to an error or exception.
  137. */
  138. @property (nonatomic, assign, readonly, getter=isFaulted) BOOL faulted;
  139. /*!
  140. Whether this task has completed.
  141. */
  142. @property (nonatomic, assign, readonly, getter=isCompleted) BOOL completed;
  143. /*!
  144. Enqueues the given block to be run once this task is complete.
  145. This method uses a default execution strategy. The block will be
  146. run on the thread where the previous task completes, unless the
  147. the stack depth is too deep, in which case it will be run on a
  148. dispatch queue with default priority.
  149. @param block The block to be run once this task is complete.
  150. @returns A task that will be completed after block has run.
  151. If block returns a BFTask, then the task returned from
  152. this method will not be completed until that task is completed.
  153. */
  154. - (BFTask *)continueWithBlock:(BFContinuationBlock)block;
  155. /*!
  156. Enqueues the given block to be run once this task is complete.
  157. This method uses a default execution strategy. The block will be
  158. run on the thread where the previous task completes, unless the
  159. the stack depth is too deep, in which case it will be run on a
  160. dispatch queue with default priority.
  161. @param block The block to be run once this task is complete.
  162. @param cancellationToken The cancellation token (optional).
  163. @returns A task that will be completed after block has run.
  164. If block returns a BFTask, then the task returned from
  165. this method will not be completed until that task is completed.
  166. */
  167. - (BFTask *)continueWithBlock:(BFContinuationBlock)block cancellationToken:(nullable BFCancellationToken *)cancellationToken;
  168. /*!
  169. Enqueues the given block to be run once this task is complete.
  170. @param executor A BFExecutor responsible for determining how the
  171. continuation block will be run.
  172. @param block The block to be run once this task is complete.
  173. @returns A task that will be completed after block has run.
  174. If block returns a BFTask, then the task returned from
  175. this method will not be completed until that task is completed.
  176. */
  177. - (BFTask *)continueWithExecutor:(BFExecutor *)executor withBlock:(BFContinuationBlock)block;
  178. /*!
  179. Enqueues the given block to be run once this task is complete.
  180. @param executor A BFExecutor responsible for determining how the
  181. continuation block will be run.
  182. @param block The block to be run once this task is complete.
  183. @param cancellationToken The cancellation token (optional).
  184. @returns A task that will be completed after block has run.
  185. If block returns a BFTask, then the task returned from
  186. his method will not be completed until that task is completed.
  187. */
  188. - (BFTask *)continueWithExecutor:(BFExecutor *)executor
  189. block:(BFContinuationBlock)block
  190. cancellationToken:(nullable BFCancellationToken *)cancellationToken;
  191. /*!
  192. Identical to continueWithBlock:, except that the block is only run
  193. if this task did not produce a cancellation, error, or exception.
  194. If it did, then the failure will be propagated to the returned
  195. task.
  196. @param block The block to be run once this task is complete.
  197. @returns A task that will be completed after block has run.
  198. If block returns a BFTask, then the task returned from
  199. this method will not be completed until that task is completed.
  200. */
  201. - (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block;
  202. /*!
  203. Identical to continueWithBlock:, except that the block is only run
  204. if this task did not produce a cancellation, error, or exception.
  205. If it did, then the failure will be propagated to the returned
  206. task.
  207. @param block The block to be run once this task is complete.
  208. @param cancellationToken The cancellation token (optional).
  209. @returns A task that will be completed after block has run.
  210. If block returns a BFTask, then the task returned from
  211. this method will not be completed until that task is completed.
  212. */
  213. - (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block cancellationToken:(nullable BFCancellationToken *)cancellationToken;
  214. /*!
  215. Identical to continueWithExecutor:withBlock:, except that the block
  216. is only run if this task did not produce a cancellation, error, or
  217. exception. If it did, then the failure will be propagated to the
  218. returned task.
  219. @param executor A BFExecutor responsible for determining how the
  220. continuation block will be run.
  221. @param block The block to be run once this task is complete.
  222. @returns A task that will be completed after block has run.
  223. If block returns a BFTask, then the task returned from
  224. this method will not be completed until that task is completed.
  225. */
  226. - (BFTask *)continueWithExecutor:(BFExecutor *)executor withSuccessBlock:(BFContinuationBlock)block;
  227. /*!
  228. Identical to continueWithExecutor:withBlock:, except that the block
  229. is only run if this task did not produce a cancellation, error, or
  230. exception. If it did, then the failure will be propagated to the
  231. returned task.
  232. @param executor A BFExecutor responsible for determining how the
  233. continuation block will be run.
  234. @param block The block to be run once this task is complete.
  235. @param cancellationToken The cancellation token (optional).
  236. @returns A task that will be completed after block has run.
  237. If block returns a BFTask, then the task returned from
  238. this method will not be completed until that task is completed.
  239. */
  240. - (BFTask *)continueWithExecutor:(BFExecutor *)executor
  241. successBlock:(BFContinuationBlock)block
  242. cancellationToken:(nullable BFCancellationToken *)cancellationToken;
  243. /*!
  244. Waits until this operation is completed.
  245. This method is inefficient and consumes a thread resource while
  246. it's running. It should be avoided. This method logs a warning
  247. message if it is used on the main thread.
  248. */
  249. - (void)waitUntilFinished;
  250. @end
  251. NS_ASSUME_NONNULL_END