FIRDatabaseQuery.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #import "FIRDatabaseSwiftNameSupport.h"
  18. #import "FIRDataEventType.h"
  19. #import "FIRDataSnapshot.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. /**
  22. * A FIRDatabaseHandle is used to identify listeners of Firebase Database events. These handles
  23. * are returned by observeEventType: and and can later be passed to removeObserverWithHandle: to
  24. * stop receiving updates.
  25. */
  26. typedef NSUInteger FIRDatabaseHandle FIR_SWIFT_NAME(DatabaseHandle);
  27. /**
  28. * A FIRDatabaseQuery instance represents a query over the data at a particular location.
  29. *
  30. * You create one by calling one of the query methods (queryOrderedByChild:, queryStartingAtValue:, etc.)
  31. * on a FIRDatabaseReference. The query methods can be chained to further specify the data you are interested in
  32. * observing
  33. */
  34. FIR_SWIFT_NAME(DatabaseQuery)
  35. @interface FIRDatabaseQuery : NSObject
  36. #pragma mark - Attach observers to read data
  37. /**
  38. * observeEventType:withBlock: is used to listen for data changes at a particular location.
  39. * This is the primary way to read data from the Firebase Database. Your block will be triggered
  40. * for the initial data and again whenever the data changes.
  41. *
  42. * Use removeObserverWithHandle: to stop receiving updates.
  43. *
  44. * @param eventType The type of event to listen for.
  45. * @param block The block that should be called with initial data and updates. It is passed the data as a FIRDataSnapshot.
  46. * @return A handle used to unregister this block later using removeObserverWithHandle:
  47. */
  48. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType withBlock:(void (^)(FIRDataSnapshot *snapshot))block;
  49. /**
  50. * observeEventType:andPreviousSiblingKeyWithBlock: is used to listen for data changes at a particular location.
  51. * This is the primary way to read data from the Firebase Database. Your block will be triggered
  52. * for the initial data and again whenever the data changes. In addition, for FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  53. * FIRDataEventTypeChildChanged events, your block will be passed the key of the previous node by priority order.
  54. *
  55. * Use removeObserverWithHandle: to stop receiving updates.
  56. *
  57. * @param eventType The type of event to listen for.
  58. * @param block The block that should be called with initial data and updates. It is passed the data as a FIRDataSnapshot
  59. * and the previous child's key.
  60. * @return A handle used to unregister this block later using removeObserverWithHandle:
  61. */
  62. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot *snapshot, NSString *__nullable prevKey))block;
  63. /**
  64. * observeEventType:withBlock: is used to listen for data changes at a particular location.
  65. * This is the primary way to read data from the Firebase Database. Your block will be triggered
  66. * for the initial data and again whenever the data changes.
  67. *
  68. * The cancelBlock will be called if you will no longer receive new events due to no longer having permission.
  69. *
  70. * Use removeObserverWithHandle: to stop receiving updates.
  71. *
  72. * @param eventType The type of event to listen for.
  73. * @param block The block that should be called with initial data and updates. It is passed the data as a FIRDataSnapshot.
  74. * @param cancelBlock The block that should be called if this client no longer has permission to receive these events
  75. * @return A handle used to unregister this block later using removeObserverWithHandle:
  76. */
  77. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType withBlock:(void (^)(FIRDataSnapshot *snapshot))block withCancelBlock:(nullable void (^)(NSError* error))cancelBlock;
  78. /**
  79. * observeEventType:andPreviousSiblingKeyWithBlock: is used to listen for data changes at a particular location.
  80. * This is the primary way to read data from the Firebase Database. Your block will be triggered
  81. * for the initial data and again whenever the data changes. In addition, for FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  82. * FIRDataEventTypeChildChanged events, your block will be passed the key of the previous node by priority order.
  83. *
  84. * The cancelBlock will be called if you will no longer receive new events due to no longer having permission.
  85. *
  86. * Use removeObserverWithHandle: to stop receiving updates.
  87. *
  88. * @param eventType The type of event to listen for.
  89. * @param block The block that should be called with initial data and updates. It is passed the data as a FIRDataSnapshot
  90. * and the previous child's key.
  91. * @param cancelBlock The block that should be called if this client no longer has permission to receive these events
  92. * @return A handle used to unregister this block later using removeObserverWithHandle:
  93. */
  94. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot *snapshot, NSString *__nullable prevKey))block withCancelBlock:(nullable void (^)(NSError* error))cancelBlock;
  95. /**
  96. * This is equivalent to observeEventType:withBlock:, except the block is immediately canceled after the initial data is returned.
  97. *
  98. * @param eventType The type of event to listen for.
  99. * @param block The block that should be called. It is passed the data as a FIRDataSnapshot.
  100. */
  101. - (void)observeSingleEventOfType:(FIRDataEventType)eventType withBlock:(void (^)(FIRDataSnapshot *snapshot))block;
  102. /**
  103. * This is equivalent to observeEventType:withBlock:, except the block is immediately canceled after the initial data is returned. In addition, for FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  104. * FIRDataEventTypeChildChanged events, your block will be passed the key of the previous node by priority order.
  105. *
  106. * @param eventType The type of event to listen for.
  107. * @param block The block that should be called. It is passed the data as a FIRDataSnapshot and the previous child's key.
  108. */
  109. - (void)observeSingleEventOfType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot *snapshot, NSString *__nullable prevKey))block;
  110. /**
  111. * This is equivalent to observeEventType:withBlock:, except the block is immediately canceled after the initial data is returned.
  112. *
  113. * The cancelBlock will be called if you do not have permission to read data at this location.
  114. *
  115. * @param eventType The type of event to listen for.
  116. * @param block The block that should be called. It is passed the data as a FIRDataSnapshot.
  117. * @param cancelBlock The block that will be called if you don't have permission to access this data
  118. */
  119. - (void)observeSingleEventOfType:(FIRDataEventType)eventType withBlock:(void (^)(FIRDataSnapshot *snapshot))block withCancelBlock:(nullable void (^)(NSError* error))cancelBlock;
  120. /**
  121. * This is equivalent to observeEventType:withBlock:, except the block is immediately canceled after the initial data is returned. In addition, for FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  122. * FIRDataEventTypeChildChanged events, your block will be passed the key of the previous node by priority order.
  123. *
  124. * The cancelBlock will be called if you do not have permission to read data at this location.
  125. *
  126. * @param eventType The type of event to listen for.
  127. * @param block The block that should be called. It is passed the data as a FIRDataSnapshot and the previous child's key.
  128. * @param cancelBlock The block that will be called if you don't have permission to access this data
  129. */
  130. - (void)observeSingleEventOfType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot *snapshot, NSString *__nullable prevKey))block withCancelBlock:(nullable void (^)(NSError* error))cancelBlock;
  131. #pragma mark - Detaching observers
  132. /**
  133. * Detach a block previously attached with observeEventType:withBlock:.
  134. *
  135. * @param handle The handle returned by the call to observeEventType:withBlock: which we are trying to remove.
  136. */
  137. - (void) removeObserverWithHandle:(FIRDatabaseHandle)handle;
  138. /**
  139. * Detach all blocks previously attached to this Firebase Database location with observeEventType:withBlock:
  140. */
  141. - (void) removeAllObservers;
  142. /**
  143. * By calling `keepSynced:YES` on a location, the data for that location will automatically be downloaded and
  144. * kept in sync, even when no listeners are attached for that location. Additionally, while a location is kept
  145. * synced, it will not be evicted from the persistent disk cache.
  146. *
  147. * @param keepSynced Pass YES to keep this location synchronized, pass NO to stop synchronization.
  148. */
  149. - (void) keepSynced:(BOOL)keepSynced;
  150. #pragma mark - Querying and limiting
  151. /**
  152. * queryLimitedToFirst: is used to generate a reference to a limited view of the data at this location.
  153. * The FIRDatabaseQuery instance returned by queryLimitedToFirst: will respond to at most the first limit child nodes.
  154. *
  155. * @param limit The upper bound, inclusive, for the number of child nodes to receive events for
  156. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  157. */
  158. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit;
  159. /**
  160. * queryLimitedToLast: is used to generate a reference to a limited view of the data at this location.
  161. * The FIRDatabaseQuery instance returned by queryLimitedToLast: will respond to at most the last limit child nodes.
  162. *
  163. * @param limit The upper bound, inclusive, for the number of child nodes to receive events for
  164. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  165. */
  166. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit;
  167. /**
  168. * queryOrderBy: is used to generate a reference to a view of the data that's been sorted by the values of
  169. * a particular child key. This method is intended to be used in combination with queryStartingAtValue:,
  170. * queryEndingAtValue:, or queryEqualToValue:.
  171. *
  172. * @param key The child key to use in ordering data visible to the returned FIRDatabaseQuery
  173. * @return A FIRDatabaseQuery instance, ordered by the values of the specified child key.
  174. */
  175. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key;
  176. /**
  177. * queryOrderedByKey: is used to generate a reference to a view of the data that's been sorted by child key.
  178. * This method is intended to be used in combination with queryStartingAtValue:, queryEndingAtValue:,
  179. * or queryEqualToValue:.
  180. *
  181. * @return A FIRDatabaseQuery instance, ordered by child keys.
  182. */
  183. - (FIRDatabaseQuery *) queryOrderedByKey;
  184. /**
  185. * queryOrderedByValue: is used to generate a reference to a view of the data that's been sorted by child value.
  186. * This method is intended to be used in combination with queryStartingAtValue:, queryEndingAtValue:,
  187. * or queryEqualToValue:.
  188. *
  189. * @return A FIRDatabaseQuery instance, ordered by child value.
  190. */
  191. - (FIRDatabaseQuery *) queryOrderedByValue;
  192. /**
  193. * queryOrderedByPriority: is used to generate a reference to a view of the data that's been sorted by child
  194. * priority. This method is intended to be used in combination with queryStartingAtValue:, queryEndingAtValue:,
  195. * or queryEqualToValue:.
  196. *
  197. * @return A FIRDatabaseQuery instance, ordered by child priorities.
  198. */
  199. - (FIRDatabaseQuery *) queryOrderedByPriority;
  200. /**
  201. * queryStartingAtValue: is used to generate a reference to a limited view of the data at this location.
  202. * The FIRDatabaseQuery instance returned by queryStartingAtValue: will respond to events at nodes with a value
  203. * greater than or equal to startValue.
  204. *
  205. * @param startValue The lower bound, inclusive, for the value of data visible to the returned FIRDatabaseQuery
  206. * @return A FIRDatabaseQuery instance, limited to data with value greater than or equal to startValue
  207. */
  208. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue;
  209. /**
  210. * queryStartingAtValue:childKey: is used to generate a reference to a limited view of the data at this location.
  211. * The FIRDatabaseQuery instance returned by queryStartingAtValue:childKey will respond to events at nodes with a value
  212. * greater than startValue, or equal to startValue and with a key greater than or equal to childKey. This is most
  213. * useful when implementing pagination in a case where multiple nodes can match the startValue.
  214. *
  215. * @param startValue The lower bound, inclusive, for the value of data visible to the returned FIRDatabaseQuery
  216. * @param childKey The lower bound, inclusive, for the key of nodes with value equal to startValue
  217. * @return A FIRDatabaseQuery instance, limited to data with value greater than or equal to startValue
  218. */
  219. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue childKey:(nullable NSString *)childKey;
  220. /**
  221. * queryEndingAtValue: is used to generate a reference to a limited view of the data at this location.
  222. * The FIRDatabaseQuery instance returned by queryEndingAtValue: will respond to events at nodes with a value
  223. * less than or equal to endValue.
  224. *
  225. * @param endValue The upper bound, inclusive, for the value of data visible to the returned FIRDatabaseQuery
  226. * @return A FIRDatabaseQuery instance, limited to data with value less than or equal to endValue
  227. */
  228. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue;
  229. /**
  230. * queryEndingAtValue:childKey: is used to generate a reference to a limited view of the data at this location.
  231. * The FIRDatabaseQuery instance returned by queryEndingAtValue:childKey will respond to events at nodes with a value
  232. * less than endValue, or equal to endValue and with a key less than or equal to childKey. This is most useful when
  233. * implementing pagination in a case where multiple nodes can match the endValue.
  234. *
  235. * @param endValue The upper bound, inclusive, for the value of data visible to the returned FIRDatabaseQuery
  236. * @param childKey The upper bound, inclusive, for the key of nodes with value equal to endValue
  237. * @return A FIRDatabaseQuery instance, limited to data with value less than or equal to endValue
  238. */
  239. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue childKey:(nullable NSString *)childKey;
  240. /**
  241. * queryEqualToValue: is used to generate a reference to a limited view of the data at this location.
  242. * The FIRDatabaseQuery instance returned by queryEqualToValue: will respond to events at nodes with a value equal
  243. * to the supplied argument.
  244. *
  245. * @param value The value that the data returned by this FIRDatabaseQuery will have
  246. * @return A FIRDatabaseQuery instance, limited to data with the supplied value.
  247. */
  248. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value;
  249. /**
  250. * queryEqualToValue:childKey: is used to generate a reference to a limited view of the data at this location.
  251. * The FIRDatabaseQuery instance returned by queryEqualToValue:childKey will respond to events at nodes with a value
  252. * equal to the supplied argument and with their key equal to childKey. There will be at most one node that matches
  253. * because child keys are unique.
  254. *
  255. * @param value The value that the data returned by this FIRDatabaseQuery will have
  256. * @param childKey The name of nodes with the right value
  257. * @return A FIRDatabaseQuery instance, limited to data with the supplied value and the key.
  258. */
  259. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value childKey:(nullable NSString *)childKey;
  260. #pragma mark - Properties
  261. /**
  262. * Gets a FIRDatabaseReference for the location of this query.
  263. *
  264. * @return A FIRDatabaseReference for the location of this query.
  265. */
  266. @property (nonatomic, readonly, strong) FIRDatabaseReference * ref;
  267. @end
  268. NS_ASSUME_NONNULL_END