SDCallbackQueue.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDWebImageCompat.h"
  9. /// SDCallbackPolicy controls how we execute the block on the queue, like whether to use `dispatch_async/dispatch_sync`, check if current queue match target queue, or just invoke without any context.
  10. typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {
  11. /// When the current queue is equal to callback queue, sync/async will just invoke `block` directly without dispatch. Else it use `dispatch_async`/`dispatch_sync` to dispatch block on queue. This is useful for UIKit rendering to ensure all blocks executed in the same runloop
  12. SDCallbackPolicySafeExecute = 0,
  13. /// Follow async/sync using the correspond `dispatch_async`/`dispatch_sync` to dispatch block on queue
  14. SDCallbackPolicyDispatch = 1,
  15. /// Ignore any async/sync and just directly invoke `block` in current queue (without `dispatch_async`/`dispatch_sync`)
  16. SDCallbackPolicyInvoke = 2
  17. };
  18. /// SDCallbackQueue is a wrapper used to control how the completionBlock should perform on queues, used by our `Cache`/`Manager`/`Loader`.
  19. /// Useful when you call SDWebImage in non-main queue and want to avoid it callback into main queue, which may cause issue.
  20. @interface SDCallbackQueue : NSObject
  21. /// The shared main queue. This is the default value, has the same effect when passing `nil` to `SDWebImageContextCallbackQueue`
  22. @property (nonnull, class, readonly) SDCallbackQueue *mainQueue;
  23. /// The caller current queue. Using `dispatch_get_current_queue`. This is not a dynamic value and only keep the first call time queue.
  24. @property (nonnull, class, readonly) SDCallbackQueue *currentQueue;
  25. /// The global concurrent queue (user-initiated QoS). Using `dispatch_get_global_queue`.
  26. @property (nonnull, class, readonly) SDCallbackQueue *globalQueue;
  27. /// The current queue's callback policy, defaults to `SDCallbackPolicySafeExecute`, which behaves like the old macro `dispatch_main_async_safe`
  28. @property (assign, readwrite) SDCallbackPolicy policy;
  29. - (nonnull instancetype)init NS_UNAVAILABLE;
  30. + (nonnull instancetype)new NS_UNAVAILABLE;
  31. /// Create the callback queue with a GCD queue
  32. /// - Parameter queue: The GCD queue, should not be NULL
  33. - (nonnull instancetype)initWithDispatchQueue:(nonnull dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
  34. #pragma mark - Execution Entry
  35. /// Submits a block for execution and returns after that block finishes executing.
  36. /// - Parameter block: The block that contains the work to perform.
  37. - (void)sync:(nonnull dispatch_block_t)block;
  38. /// Schedules a block asynchronously for execution.
  39. /// - Parameter block: The block that contains the work to perform.
  40. - (void)async:(nonnull dispatch_block_t)block;
  41. @end