FSTDispatchQueue.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. @interface FSTDispatchQueue : NSObject
  19. /** Creates and returns an FSTDispatchQueue wrapping the specified dispatch_queue_t. */
  20. + (instancetype)queueWith:(dispatch_queue_t)dispatchQueue;
  21. - (instancetype)initWithQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
  22. - (instancetype)init __attribute__((unavailable("Use static constructor method.")));
  23. /**
  24. * Asserts that we are already running on this queue (actually, we can only verify that the
  25. * queue's label is the same, but hopefully that's good enough.)
  26. */
  27. - (void)verifyIsCurrentQueue;
  28. /**
  29. * Same as dispatch_async() except it asserts that we're not already on the queue, since this
  30. * generally indicates a bug (and can lead to re-ordering of operations, etc).
  31. *
  32. * @param block The block to run.
  33. */
  34. - (void)dispatchAsync:(void (^)(void))block;
  35. /**
  36. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  37. * the current one (thus it is equivalent to a raw dispatch_async()).
  38. *
  39. * This is useful, e.g. for dispatching to the user's queue directly from user API call (in which
  40. * case we don't know if we're already on the user's queue or not).
  41. *
  42. * @param block The block to run.
  43. */
  44. - (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block;
  45. /**
  46. * Schedules a callback after the specified delay.
  47. *
  48. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  49. * the current one (thus it is equivalent to a raw dispatch_after()).
  50. *
  51. * @param block The block to run.
  52. * @param delay The delay (in seconds) after which to run the block.
  53. */
  54. - (void)dispatchAfterDelay:(NSTimeInterval)delay block:(void (^)(void))block;
  55. /** The underlying wrapped dispatch_queue_t */
  56. @property(nonatomic, strong, readonly) dispatch_queue_t queue;
  57. @end
  58. NS_ASSUME_NONNULL_END