NSObject+BKBlockObservation.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // NSObject+BKBlockObservation.h
  3. // BlocksKit
  4. //
  5. #import <Foundation/Foundation.h>
  6. /** Blocks wrapper for key-value observation.
  7. In Mac OS X Panther, Apple introduced an API called "key-value
  8. observing." It implements an [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern),
  9. where an object will notify observers of any changes in state.
  10. NSNotification is a rudimentary form of this design style;
  11. KVO, however, allows for the observation of any change in key-value state.
  12. The API for key-value observation, however, is flawed, ugly, and lengthy.
  13. Like most of the other block abilities in BlocksKit, observation saves
  14. and a bunch of code and a bunch of potential bugs.
  15. Includes code by the following:
  16. - [Andy Matuschak](https://github.com/andymatuschak)
  17. - [Jon Sterling](https://github.com/jonsterling)
  18. - [Zach Waldowski](https://github.com/zwaldowski)
  19. - [Jonathan Wight](https://github.com/schwa)
  20. */
  21. @interface NSObject (BlockObservation)
  22. /** Adds an observer to an object conforming to NSKeyValueObserving.
  23. Adds a block observer that executes a block upon a state change.
  24. @param keyPath The property to observe, relative to the reciever.
  25. @param task A block with no return argument, and a single parameter: the reciever.
  26. @return Returns a globally unique process identifier for removing
  27. observation with removeObserverWithBlockToken:.
  28. @see addObserverForKeyPath:identifier:options:task:
  29. */
  30. - (NSString *)bk_addObserverForKeyPath:(NSString *)keyPath task:(void (^)(id target))task;
  31. /** Adds an observer to an object conforming to NSKeyValueObserving.
  32. Adds a block observer that executes the same block upon
  33. multiple state changes.
  34. @param keyPaths An array of properties to observe, relative to the reciever.
  35. @param task A block with no return argument and two parameters: the
  36. reciever and the key path of the value change.
  37. @return A unique identifier for removing
  38. observation with removeObserverWithBlockToken:.
  39. @see addObserverForKeyPath:identifier:options:task:
  40. */
  41. - (NSString *)bk_addObserverForKeyPaths:(NSArray *)keyPaths task:(void (^)(id obj, NSString *keyPath))task;
  42. /** Adds an observer to an object conforming to NSKeyValueObserving.
  43. Adds a block observer that executes a block upon a state change
  44. with specific options.
  45. @param keyPath The property to observe, relative to the reciever.
  46. @param options The NSKeyValueObservingOptions to use.
  47. @param task A block with no return argument and two parameters: the
  48. reciever and the change dictionary.
  49. @return Returns a globally unique process identifier for removing
  50. observation with removeObserverWithBlockToken:.
  51. @see addObserverForKeyPath:identifier:options:task:
  52. */
  53. - (NSString *)bk_addObserverForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSDictionary *change))task;
  54. /** Adds an observer to an object conforming to NSKeyValueObserving.
  55. Adds a block observer that executes the same block upon
  56. multiple state changes with specific options.
  57. @param keyPaths An array of properties to observe, relative to the reciever.
  58. @param options The NSKeyValueObservingOptions to use.
  59. @param task A block with no return argument and three parameters: the
  60. reciever, the key path of the value change, and the change dictionary.
  61. @return A unique identifier for removing
  62. observation with removeObserverWithBlockToken:.
  63. @see addObserverForKeyPath:identifier:options:task:
  64. */
  65. - (NSString *)bk_addObserverForKeyPaths:(NSArray *)keyPaths options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSString *keyPath, NSDictionary *change))task;
  66. /** Adds an observer to an object conforming to NSKeyValueObserving.
  67. Adds a block observer that executes the block upon a
  68. state change.
  69. @param keyPath The property to observe, relative to the reciever.
  70. @param token An identifier for the observation block.
  71. @param options The NSKeyValueObservingOptions to use.
  72. @param task A block responding to the reciever and the KVO change.
  73. observation with removeObserverWithBlockToken:.
  74. @see addObserverForKeyPath:task:
  75. */
  76. - (void)bk_addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)token options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSDictionary *change))task;
  77. /** Adds an observer to an object conforming to NSKeyValueObserving.
  78. Adds a block observer that executes the same block upon
  79. multiple state changes.
  80. @param keyPaths An array of properties to observe, relative to the reciever.
  81. @param token An identifier for the observation block.
  82. @param options The NSKeyValueObservingOptions to use.
  83. @param task A block responding to the reciever, the key path, and the KVO change.
  84. observation with removeObserversWithIdentifier:.
  85. @see addObserverForKeyPath:task:
  86. */
  87. - (void)bk_addObserverForKeyPaths:(NSArray *)keyPaths identifier:(NSString *)token options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSString *keyPath, NSDictionary *change))task;
  88. /** Removes a block observer.
  89. @param keyPath The property to stop observing, relative to the reciever.
  90. @param token The unique key returned by addObserverForKeyPath:task:
  91. or the identifier given in addObserverForKeyPath:identifier:task:.
  92. @see removeObserversWithIdentifier:
  93. */
  94. - (void)bk_removeObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)token;
  95. /** Removes multiple block observers with a certain identifier.
  96. @param token A unique key returned by addObserverForKeyPath:task:
  97. and addObserverForKeyPaths:task: or the identifier given in
  98. addObserverForKeyPath:identifier:task: and
  99. addObserverForKeyPaths:identifier:task:.
  100. @see removeObserverForKeyPath:identifier:
  101. */
  102. - (void)bk_removeObserversWithIdentifier:(NSString *)token;
  103. /** Remove all registered block observers. */
  104. - (void)bk_removeAllBlockObservers;
  105. @end