FIRDatabaseQuery.h 16 KB

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