RACScheduler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // RACScheduler.h
  3. // ReactiveCocoa
  4. //
  5. // Created by Josh Abernathy on 4/16/12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /// The priority for the scheduler.
  10. ///
  11. /// RACSchedulerPriorityHigh - High priority.
  12. /// RACSchedulerPriorityDefault - Default priority.
  13. /// RACSchedulerPriorityLow - Low priority.
  14. /// RACSchedulerPriorityBackground - Background priority.
  15. typedef enum : long {
  16. RACSchedulerPriorityHigh = DISPATCH_QUEUE_PRIORITY_HIGH,
  17. RACSchedulerPriorityDefault = DISPATCH_QUEUE_PRIORITY_DEFAULT,
  18. RACSchedulerPriorityLow = DISPATCH_QUEUE_PRIORITY_LOW,
  19. RACSchedulerPriorityBackground = DISPATCH_QUEUE_PRIORITY_BACKGROUND,
  20. } RACSchedulerPriority;
  21. /// Scheduled with -scheduleRecursiveBlock:, this type of block is passed a block
  22. /// with which it can call itself recursively.
  23. typedef void (^RACSchedulerRecursiveBlock)(void (^reschedule)(void));
  24. @class RACDisposable;
  25. /// Schedulers are used to control when and where work is performed.
  26. @interface RACScheduler : NSObject
  27. /// A singleton scheduler that immediately executes the blocks it is given.
  28. ///
  29. /// **Note:** Unlike most other schedulers, this does not set the current
  30. /// scheduler. There may still be a valid +currentScheduler if this is used
  31. /// within a block scheduled on a different scheduler.
  32. + (RACScheduler *)immediateScheduler;
  33. /// A singleton scheduler that executes blocks in the main thread.
  34. + (RACScheduler *)mainThreadScheduler;
  35. /// Creates and returns a new background scheduler with the given priority and
  36. /// name. The name is for debug and instrumentation purposes only.
  37. ///
  38. /// Scheduler creation is cheap. It's unnecessary to save the result of this
  39. /// method call unless you want to serialize some actions on the same background
  40. /// scheduler.
  41. + (RACScheduler *)schedulerWithPriority:(RACSchedulerPriority)priority name:(NSString *)name;
  42. /// Invokes +schedulerWithPriority:name: with a default name.
  43. + (RACScheduler *)schedulerWithPriority:(RACSchedulerPriority)priority;
  44. /// Invokes +schedulerWithPriority: with RACSchedulerPriorityDefault.
  45. + (RACScheduler *)scheduler;
  46. /// The current scheduler. This will only be valid when used from within a
  47. /// -[RACScheduler schedule:] block or when on the main thread.
  48. + (RACScheduler *)currentScheduler;
  49. /// Schedule the given block for execution on the scheduler.
  50. ///
  51. /// Scheduled blocks will be executed in the order in which they were scheduled.
  52. ///
  53. /// block - The block to schedule for execution. Cannot be nil.
  54. ///
  55. /// Returns a disposable which can be used to cancel the scheduled block before
  56. /// it begins executing, or nil if cancellation is not supported.
  57. - (RACDisposable *)schedule:(void (^)(void))block;
  58. /// Schedule the given block for execution on the scheduler at or after
  59. /// a specific time.
  60. ///
  61. /// Note that blocks scheduled for a certain time will not preempt any other
  62. /// scheduled work that is executing at the time.
  63. ///
  64. /// When invoked on the +immediateScheduler, the calling thread **will block**
  65. /// until the specified time.
  66. ///
  67. /// date - The earliest time at which `block` should begin executing. The block
  68. /// may not execute immediately at this time, whether due to system load
  69. /// or another block on the scheduler currently being run. Cannot be nil.
  70. /// block - The block to schedule for execution. Cannot be nil.
  71. ///
  72. /// Returns a disposable which can be used to cancel the scheduled block before
  73. /// it begins executing, or nil if cancellation is not supported.
  74. - (RACDisposable *)after:(NSDate *)date schedule:(void (^)(void))block;
  75. /// Schedule the given block for execution on the scheduler after the delay.
  76. ///
  77. /// Converts the delay into an NSDate, then invokes `-after:schedule:`.
  78. - (RACDisposable *)afterDelay:(NSTimeInterval)delay schedule:(void (^)(void))block;
  79. /// Reschedule the given block at a particular interval, starting at a specific
  80. /// time, and with a given leeway for deferral.
  81. ///
  82. /// Note that blocks scheduled for a certain time will not preempt any other
  83. /// scheduled work that is executing at the time.
  84. ///
  85. /// Regardless of the value of `leeway`, the given block may not execute exactly
  86. /// at `when` or exactly on successive intervals, whether due to system load or
  87. /// because another block is currently being run on the scheduler.
  88. ///
  89. /// It is considered undefined behavior to invoke this method on the
  90. /// +immediateScheduler.
  91. ///
  92. /// date - The earliest time at which `block` should begin executing. The
  93. /// block may not execute immediately at this time, whether due to
  94. /// system load or another block on the scheduler currently being
  95. /// run. Cannot be nil.
  96. /// interval - The interval at which the block should be rescheduled, starting
  97. /// from `date`. This will use the system wall clock, to avoid
  98. /// skew when the computer goes to sleep.
  99. /// leeway - A hint to the system indicating the number of seconds that each
  100. /// scheduling can be deferred. Note that this is just a hint, and
  101. /// there may be some additional latency no matter what.
  102. /// block - The block to repeatedly schedule for execution. Cannot be nil.
  103. ///
  104. /// Returns a disposable which can be used to cancel the automatic scheduling and
  105. /// rescheduling, or nil if cancellation is not supported.
  106. - (RACDisposable *)after:(NSDate *)date repeatingEvery:(NSTimeInterval)interval withLeeway:(NSTimeInterval)leeway schedule:(void (^)(void))block;
  107. /// Schedule the given recursive block for execution on the scheduler. The
  108. /// scheduler will automatically flatten any recursive scheduling into iteration
  109. /// instead, so this can be used without issue for blocks that may keep invoking
  110. /// themselves forever.
  111. ///
  112. /// Scheduled blocks will be executed in the order in which they were scheduled.
  113. ///
  114. /// recursiveBlock - The block to schedule for execution. When invoked, the
  115. /// recursive block will be passed a `void (^)(void)` block
  116. /// which will reschedule the recursive block at the end of the
  117. /// receiver's queue. This passed-in block will automatically
  118. /// skip scheduling if the scheduling of the `recursiveBlock`
  119. /// was disposed in the meantime.
  120. ///
  121. /// Returns a disposable which can be used to cancel the scheduled block before
  122. /// it begins executing, or to stop it from rescheduling if it's already begun
  123. /// execution.
  124. - (RACDisposable *)scheduleRecursiveBlock:(RACSchedulerRecursiveBlock)recursiveBlock;
  125. @end
  126. @interface RACScheduler (Unavailable)
  127. + (RACScheduler *)schedulerWithQueue:(dispatch_queue_t)queue name:(NSString *)name __attribute__((unavailable("Use -[RACTargetQueueScheduler initWithName:targetQueue:] instead.")));
  128. @end