FIRDatabaseQuery.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 "FIRDataEventType.h"
  17. #import "FIRDataSnapshot.h"
  18. #import <Foundation/Foundation.h>
  19. NS_ASSUME_NONNULL_BEGIN
  20. /**
  21. * A `DatabaseHandle` is used to identify listeners of Firebase Database
  22. * events. These handles are returned by `observe(_:with:)` and can later be
  23. * passed to `removeObserver(withHandle:)` to stop receiving updates.
  24. */
  25. typedef NSUInteger FIRDatabaseHandle NS_SWIFT_NAME(DatabaseHandle);
  26. /**
  27. * A `DatabaseQuery` instance represents a query over the data at a particular
  28. * location.
  29. *
  30. * You create one by calling one of the query methods (`queryOrdered(byChild:)`,
  31. * `queryStarting(atValue:)`, etc.) on a `DatabaseReference`. The query methods
  32. * can be chained to further specify the data you are interested in observing.
  33. */
  34. NS_SWIFT_NAME(DatabaseQuery)
  35. @interface FIRDatabaseQuery : NSObject
  36. #pragma mark - Attach observers to read data
  37. /**
  38. * This method 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
  40. * block will be triggered for the initial data and again whenever the
  41. * data changes.
  42. *
  43. * Use removeObserverWithHandle: to stop receiving updates.
  44. *
  45. * @param eventType The type of event to listen for.
  46. * @param block The block that should be called with initial data and updates.
  47. * It is passed the data as a `DataSnapshot`.
  48. * @return A handle used to unregister this block later using
  49. * `removeObserver(withHandle:)`
  50. */
  51. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  52. withBlock:
  53. (void (^)(FIRDataSnapshot *snapshot))block;
  54. /**
  55. * This method is used to listen for data changes at a particular location.
  56. * This is the primary way to read data from the Firebase Database. Your
  57. * block will be triggered for the initial data and again whenever the data
  58. * changes. In addition, for `DataEventTypeChildAdded`,
  59. * `DataEventTypeChildMoved`, and `DataEventTypeChildChanged` events, your
  60. * block will be passed the key of the previous node by priority order.
  61. *
  62. * Use `removeObserver(withHandle:)` to stop receiving updates.
  63. *
  64. * @param eventType The type of event to listen for.
  65. * @param block The block that should be called with initial data and updates.
  66. * It is passed the data as a `DataSnapshot` and the previous child's key.
  67. * @return A handle used to unregister this block later using
  68. * `removeObserver(withHandle:)`
  69. */
  70. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  71. andPreviousSiblingKeyWithBlock:
  72. (void (^)(FIRDataSnapshot *snapshot,
  73. NSString *__nullable prevKey))block;
  74. /**
  75. * This method is used to listen for data changes at a particular location.
  76. * This is the primary way to read data from the Firebase Database. Your
  77. * block will be triggered for the initial data and again whenever the data
  78. * changes.
  79. *
  80. * The `cancelBlock` will be called if you will no longer receive new events
  81. * due to no longer having permission.
  82. *
  83. * Use `removeObserver(withHandle:)` to stop receiving updates.
  84. *
  85. * @param eventType The type of event to listen for.
  86. * @param block The block that should be called with initial data and updates.
  87. * It is passed the data as a `DataSnapshot`.
  88. * @param cancelBlock The block that should be called if this client no longer
  89. * has permission to receive these events
  90. * @return A handle used to unregister this block later using
  91. * `removeObserver(withHandle:)`
  92. */
  93. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  94. withBlock:(void (^)(FIRDataSnapshot *snapshot))block
  95. withCancelBlock:
  96. (nullable void (^)(NSError *error))cancelBlock;
  97. /**
  98. * This method is used to listen for data changes at a particular location.
  99. * This is the primary way to read data from the Firebase Database. Your block
  100. * will be triggered for the initial data and again whenever the data changes.
  101. * In addition, for `FIRDataEventTypeChildAdded`, `FIRDataEventTypeChildMoved`,
  102. * and FIRDataEventTypeChildChanged events, your block will be passed the key
  103. * of the previous node by priority order.
  104. *
  105. * The `cancelBlock` will be called if you will no longer receive new events due
  106. * to no longer having permission.
  107. *
  108. * Use `removeObserver(withHandle:)` to stop receiving updates.
  109. *
  110. * @param eventType The type of event to listen for.
  111. * @param block The block that should be called with initial data and updates.
  112. * It is passed the data as a `DataSnapshot` and the previous child's key.
  113. * @param cancelBlock The block that should be called if this client no longer
  114. * has permission to receive these events
  115. * @return A handle used to unregister this block later using
  116. * `removeObserver(withHandle:)`
  117. */
  118. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  119. andPreviousSiblingKeyWithBlock:
  120. (void (^)(FIRDataSnapshot *snapshot,
  121. NSString *__nullable prevKey))block
  122. withCancelBlock:
  123. (nullable void (^)(NSError *error))cancelBlock;
  124. /**
  125. * This method is used to get the most up-to-date value for this query. This
  126. * method updates the cache and raises events if successful. If
  127. * not connected, it returns a locally-cached value.
  128. *
  129. * @param block The block that should be called with the most up-to-date value
  130. * of this query, or an error if no such value could be retrieved.
  131. */
  132. - (void)getDataWithCompletionBlock:
  133. (void (^_Nonnull)(NSError *__nullable error,
  134. FIRDataSnapshot *__nullable snapshot))block
  135. NS_SWIFT_NAME(getData(completion:));
  136. /**
  137. * This is equivalent to `observe(_:with:)`, except the block is
  138. * immediately canceled after the initial data is returned.
  139. *
  140. * @param eventType The type of event to listen for.
  141. * @param block The block that should be called. It is passed the data as a
  142. * `DataSnapshot`.
  143. */
  144. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  145. withBlock:(void (^)(FIRDataSnapshot *snapshot))block;
  146. /**
  147. * This is equivalent to `observe(_:with:)`, except the block is
  148. * immediately canceled after the initial data is returned. In addition, for
  149. * `DataEventTypeChildAdded`, `DataEventTypeChildMoved`, and
  150. * `DataEventTypeChildChanged` events, your block will be passed the key of the
  151. * previous node by priority order.
  152. *
  153. * @param eventType The type of event to listen for.
  154. * @param block The block that should be called. It is passed the data as a
  155. * `DataSnapshot` and the previous child's key.
  156. */
  157. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  158. andPreviousSiblingKeyWithBlock:
  159. (void (^)(FIRDataSnapshot *snapshot,
  160. NSString *__nullable prevKey))block;
  161. /**
  162. * This is equivalent to `observe(_:with:)`, except the block is
  163. * immediately canceled after the initial data is returned.
  164. *
  165. * The `cancelBlock` will be called if you do not have permission to read data
  166. * at this location.
  167. *
  168. * @param eventType The type of event to listen for.
  169. * @param block The block that should be called. It is passed the data as a
  170. * `DataSnapshot`.
  171. * @param cancelBlock The block that will be called if you don't have permission
  172. * to access this data
  173. */
  174. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  175. withBlock:(void (^)(FIRDataSnapshot *snapshot))block
  176. withCancelBlock:(nullable void (^)(NSError *error))cancelBlock;
  177. /**
  178. * This is equivalent to `observe(_:with:)`, except the block is
  179. * immediately canceled after the initial data is returned. In addition, for
  180. * `DataEventTypeChildAdded`, `DataEventTypeChildMoved`, and
  181. * `DataEventTypeChildChanged` events, your block will be passed the key of the
  182. * previous node by priority order.
  183. *
  184. * The `cancelBlock` will be called if you do not have permission to read data
  185. * at this location.
  186. *
  187. * @param eventType The type of event to listen for.
  188. * @param block The block that should be called. It is passed the data as a
  189. * `DataSnapshot` and the previous child's key.
  190. * @param cancelBlock The block that will be called if you don't have permission
  191. * to access this data
  192. */
  193. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  194. andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot *snapshot,
  195. NSString *__nullable prevKey))block
  196. withCancelBlock:
  197. (nullable void (^)(NSError *error))cancelBlock;
  198. #pragma mark - Detaching observers
  199. /**
  200. * Detach a block previously attached with `observe(_:with:)`, or another query
  201. * observation method. After this method is called, the associated block
  202. * registered to receive snapshot updates will no longer be invoked.
  203. *
  204. * @param handle The handle returned by the call to `observe(_:with:)`
  205. * which we are trying to remove.
  206. */
  207. - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle;
  208. /**
  209. * Detach all blocks previously attached to this Firebase Database location with
  210. * `observe(_:with:)` and other query observation methods.
  211. */
  212. - (void)removeAllObservers;
  213. /**
  214. * By calling `keepSynced(true)` on a location, the data for that location will
  215. * automatically be downloaded and kept in sync, even when no listeners are
  216. * attached for that location. Additionally, while a location is kept synced, it
  217. * will not be evicted from the persistent disk cache.
  218. *
  219. * @param keepSynced Pass true to keep this location synchronized, or false to
  220. * stop synchronization.
  221. */
  222. - (void)keepSynced:(BOOL)keepSynced;
  223. #pragma mark - Querying and limiting
  224. /**
  225. * This method is used to generate a reference to a limited view of the
  226. * data at this location. The `DatabaseQuery` instance returned by
  227. * `queryLimited(toFirst:)` will respond to at most the first limit child nodes.
  228. *
  229. * @param limit The upper bound, inclusive, for the number of child nodes to
  230. * receive events for
  231. * @return A `DatabaseQuery` instance, limited to at most limit child nodes.
  232. */
  233. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit;
  234. /**
  235. * `queryLimited(toLast:)` is used to generate a reference to a limited view of
  236. * the data at this location. The `DatabaseQuery` instance returned by
  237. * this method will respond to at most the last limit child nodes.
  238. *
  239. * @param limit The upper bound, inclusive, for the number of child nodes to
  240. * receive events for
  241. * @return A `DatabaseQuery` instance, limited to at most limit child nodes.
  242. */
  243. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit;
  244. /**
  245. * This method is used to generate a reference to a view of the data that's
  246. * been sorted by the values of a particular child key. This method is intended
  247. * to be used in combination with `queryStarting(atValue:)`,
  248. * `queryEnding(atValue:)`, or `queryEqual(toValue:)`.
  249. *
  250. * @param key The child key to use in ordering data visible to the returned
  251. * `DatabaseQuery`
  252. * @return A `DatabaseQuery` instance, ordered by the values of the specified
  253. * child key.
  254. */
  255. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key;
  256. /**
  257. * `queryOrdered(byKey:) is used to generate a reference to a view of the data
  258. * that's been sorted by child key. This method is intended to be used in
  259. * combination with `queryStarting(atValue:)`, `queryEnding(atValue:)`, or
  260. * `queryEqual(toValue:)`.
  261. *
  262. * @return A `DatabaseQuery` instance, ordered by child keys.
  263. */
  264. - (FIRDatabaseQuery *)queryOrderedByKey;
  265. /**
  266. * `queryOrdered(byValue:)` is used to generate a reference to a view of the
  267. * data that's been sorted by child value. This method is intended to be used in
  268. * combination with `queryStarting(atValue:)`, `queryEnding(atValue:)`, or
  269. * `queryEqual(toValue:)`.
  270. *
  271. * @return A `DatabaseQuery` instance, ordered by child value.
  272. */
  273. - (FIRDatabaseQuery *)queryOrderedByValue;
  274. /**
  275. * `queryOrdered(byPriority:) is used to generate a reference to a view of the
  276. * data that's been sorted by child priority. This method is intended to be used
  277. * in combination with `queryStarting(atValue:)`, `queryEnding(atValue:)`, or
  278. * `queryEqual(toValue:)`.
  279. *
  280. * @return A `DatabaseQuery` instance, ordered by child priorities.
  281. */
  282. - (FIRDatabaseQuery *)queryOrderedByPriority;
  283. /**
  284. * `queryStarting(atValue:)` is used to generate a reference to a limited view
  285. * of the data at this location. The `DatabaseQuery` instance returned by
  286. * `queryStarting(atValue:)` will respond to events at nodes with a value
  287. * greater than or equal to `startValue`.
  288. *
  289. * @param startValue The lower bound, inclusive, for the value of data visible
  290. * to the returned `DatabaseQuery`
  291. * @return A `DatabaseQuery` instance, limited to data with value greater than
  292. * or equal to `startValue`
  293. */
  294. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue;
  295. /**
  296. * `queryStarting(atValue:childKey:)` is used to generate a reference to a
  297. * limited view of the data at this location. The `DatabaseQuery` instance
  298. * returned by `queryStarting(atValue:childKey:)` will respond to events at
  299. * nodes with a value greater than `startValue`, or equal to `startValue` and
  300. * with a key greater than or equal to `childKey`. This is most useful when
  301. * implementing pagination in a case where multiple nodes can match the
  302. * `startValue`.
  303. *
  304. * @param startValue The lower bound, inclusive, for the value of data visible
  305. * to the returned `DatabaseQuery`
  306. * @param childKey The lower bound, inclusive, for the key of nodes with value
  307. * equal to `startValue`
  308. * @return A `DatabaseQuery` instance, limited to data with value greater than
  309. * or equal to `startValue`
  310. */
  311. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue
  312. childKey:(nullable NSString *)childKey;
  313. /**
  314. * `queryStarting(afterValue:)` is used to generate a reference to a
  315. * limited view of the data at this location. The `DatabaseQuery` instance
  316. * returned by `queryStarting(afterValue:)` will respond to events at nodes
  317. * with a value greater than startAfterValue.
  318. *
  319. * @param startAfterValue The lower bound, exclusive, for the value of data
  320. * visible to the returned `DatabaseQuery`
  321. * @return A `DatabaseQuery` instance, limited to data with value greater
  322. * `startAfterValue`
  323. */
  324. - (FIRDatabaseQuery *)queryStartingAfterValue:(nullable id)startAfterValue;
  325. /**
  326. * `queryStarting(afterValue:childKey:)` is used to generate a reference to a
  327. * limited view of the data at this location. The `DatabaseQuery` instance
  328. * returned by `queryStarting(afterValue:childKey:)` will respond to events at
  329. * nodes with a value greater than `startAfterValue`, or equal to
  330. * `startAfterValue` and with a key greater than `childKey`. This is most useful
  331. * when implementing pagination in a case where multiple nodes can match the
  332. * `startAfterValue`.
  333. *
  334. * @param startAfterValue The lower bound, inclusive, for the value of data
  335. * visible to the returned `DatabaseQuery`
  336. * @param childKey The lower bound, exclusive, for the key of nodes with value
  337. * equal to `startAfterValue`
  338. * @return A `DatabaseQuery` instance, limited to data with value greater than
  339. * `startAfterValue`, or equal to `startAfterValue` with a key greater than
  340. * `childKey`
  341. */
  342. - (FIRDatabaseQuery *)queryStartingAfterValue:(nullable id)startAfterValue
  343. childKey:(nullable NSString *)childKey;
  344. /**
  345. * `queryEnding(atValue:)` is used to generate a reference to a limited view of
  346. * the data at this location. The DatabaseQuery instance returned by
  347. * `queryEnding(atValue:)` will respond to events at nodes with a value less
  348. * than or equal to `endValue`.
  349. *
  350. * @param endValue The upper bound, inclusive, for the value of data visible to
  351. * the returned `DatabaseQuery`
  352. * @return A `DatabaseQuery` instance, limited to data with value less than or
  353. * equal to `endValue`
  354. */
  355. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue;
  356. /**
  357. * `queryEnding(atValue:childKey:)` is used to generate a reference to a limited
  358. * view of the data at this location. The `DatabaseQuery` instance returned by
  359. * `queryEnding(atValue:childKey:)` will respond to events at nodes with a value
  360. * less than `endValue`, or equal to `endValue` and with a key less than or
  361. * equal to `childKey`. This is most useful when implementing pagination in a
  362. * case where multiple nodes can match the `endValue`.
  363. *
  364. * @param endValue The upper bound, inclusive, for the value of data visible to
  365. * the returned `DatabaseQuery`
  366. * @param childKey The upper bound, inclusive, for the key of nodes with value
  367. * equal to `endValue`
  368. * @return A `DatabaseQuery` instance, limited to data with value less than or
  369. * equal to `endValue`
  370. */
  371. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue
  372. childKey:(nullable NSString *)childKey;
  373. /**
  374. * `queryEnding(beforeValue:) is used to generate a reference to a limited view
  375. * of the data at this location. The `DatabaseQuery` instance returned by
  376. * `queryEnding(beforeValue:)` will respond to events at nodes with a value less
  377. * than `endValue`.
  378. *
  379. * @param endValue The upper bound, exclusive, for the value of data visible to
  380. * the returned `DatabaseQuery`
  381. * @return A `DatabaseQuery` instance, limited to data with value less than
  382. * `endValue`
  383. */
  384. - (FIRDatabaseQuery *)queryEndingBeforeValue:(nullable id)endValue;
  385. /**
  386. * `queryEnding(beforeValue:childKey:)` is used to generate a reference to a
  387. * limited view of the data at this location. The `DatabaseQuery` instance
  388. * returned by `queryEnding(beforeValue:childKey:)` will respond to events at
  389. * nodes with a value less than `endValue`, or equal to `endValue` and with a
  390. * key less than childKey.
  391. *
  392. * @param endValue The upper bound, inclusive, for the value of data visible to
  393. * the returned `DatabaseQuery`
  394. * @param childKey The upper bound, exclusive, for the key of nodes with value
  395. * equal to endValue
  396. * @return A `DatabaseQuery` instance, limited to data with value less than or
  397. * equal to endValue
  398. */
  399. - (FIRDatabaseQuery *)queryEndingBeforeValue:(nullable id)endValue
  400. childKey:(nullable NSString *)childKey;
  401. /**
  402. * `queryEqual(toValue:)` is used to generate a reference to a limited view of
  403. * the data at this location. The `DatabaseQuery` instance returned by
  404. * `queryEqual(toValue:)` will respond to events at nodes with a value equal to
  405. * the supplied argument.
  406. *
  407. * @param value The value that the data returned by this `DatabaseQuery` will
  408. * have
  409. * @return A `DatabaseQuery` instance, limited to data with the supplied value.
  410. */
  411. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value;
  412. /**
  413. * `queryEqual(toValue:childKey:)` is used to generate a reference to a limited
  414. * view of the data at this location. The `DatabaseQuery` instance returned by
  415. * `queryEqual(toValue:childKey:)` will respond to events at nodes with a value
  416. * equal to the supplied argument and with their key equal to `childKey`. There
  417. * will be at most one node that matches because child keys are unique.
  418. *
  419. * @param value The value that the data returned by this `DatabaseQuery` will
  420. * have
  421. * @param childKey The name of nodes with the right value
  422. * @return A `DatabaseQuery` instance, limited to data with the supplied value
  423. * and the key.
  424. */
  425. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value
  426. childKey:(nullable NSString *)childKey;
  427. #pragma mark - Properties
  428. /**
  429. * Gets a `DatabaseReference` for the location of this query.
  430. *
  431. * @return A `DatabaseReference` for the location of this query.
  432. */
  433. @property(nonatomic, readonly, strong) FIRDatabaseReference *ref;
  434. @end
  435. NS_ASSUME_NONNULL_END