FIRDatabaseQuery.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 FIRDatabaseHandle is used to identify listeners of Firebase Database
  22. * events. These handles are returned by observeEventType: and can later be
  23. * passed to removeObserverWithHandle: to stop 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
  28. * location.
  29. *
  30. * You create one by calling one of the query methods (queryOrderedByChild:,
  31. * queryStartingAtValue:, etc.) on a FIRDatabaseReference. The query methods can
  32. * 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. * observeEventType:withBlock: is used to listen for data changes at a
  39. * particular location. This is the primary way to read data from the Firebase
  40. * Database. Your block will be triggered for the initial data and again
  41. * whenever the 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 FIRDataSnapshot.
  48. * @return A handle used to unregister this block later using
  49. * removeObserverWithHandle:
  50. */
  51. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  52. withBlock:
  53. (void (^)(FIRDataSnapshot *snapshot))block;
  54. /**
  55. * observeEventType:andPreviousSiblingKeyWithBlock: is used to listen for data
  56. * changes at a particular location. This is the primary way to read data from
  57. * the Firebase Database. Your block will be triggered for the initial data and
  58. * again whenever the data changes. In addition, for FIRDataEventTypeChildAdded,
  59. * FIRDataEventTypeChildMoved, and FIRDataEventTypeChildChanged events, your
  60. * block will be passed the key of the previous node by priority order.
  61. *
  62. * Use removeObserverWithHandle: 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 FIRDataSnapshot and the previous child's key.
  67. * @return A handle used to unregister this block later using
  68. * removeObserverWithHandle:
  69. */
  70. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  71. andPreviousSiblingKeyWithBlock:
  72. (void (^)(FIRDataSnapshot *snapshot,
  73. NSString *__nullable prevKey))block;
  74. /**
  75. * observeEventType:withBlock: is used to listen for data changes at a
  76. * particular location. This is the primary way to read data from the Firebase
  77. * Database. Your block will be triggered for the initial data and again
  78. * whenever the data changes.
  79. *
  80. * The cancelBlock will be called if you will no longer receive new events due
  81. * to no longer having permission.
  82. *
  83. * Use removeObserverWithHandle: 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 FIRDataSnapshot.
  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. * removeObserverWithHandle:
  92. */
  93. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  94. withBlock:(void (^)(FIRDataSnapshot *snapshot))block
  95. withCancelBlock:
  96. (nullable void (^)(NSError *error))cancelBlock;
  97. /**
  98. * observeEventType:andPreviousSiblingKeyWithBlock: is used to listen for data
  99. * changes at a particular location. This is the primary way to read data from
  100. * the Firebase Database. Your block will be triggered for the initial data and
  101. * again whenever the data changes. In addition, for FIRDataEventTypeChildAdded,
  102. * FIRDataEventTypeChildMoved, and FIRDataEventTypeChildChanged events, your
  103. * block will be passed the key 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 removeObserverWithHandle: 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 FIRDataSnapshot 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. * removeObserverWithHandle:
  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. * getDataWithCompletionBlock: is used to get the most up-to-date value for
  126. * this query. This method updates the cache and raises events if successful. If
  127. * not connected, falls back to 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 *snapshot))block
  135. NS_SWIFT_NAME(getData(completion:));
  136. /**
  137. * This is equivalent to observeEventType:withBlock:, 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. * FIRDataSnapshot.
  143. */
  144. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  145. withBlock:(void (^)(FIRDataSnapshot *snapshot))block;
  146. /**
  147. * This is equivalent to observeEventType:withBlock:, except the block is
  148. * immediately canceled after the initial data is returned. In addition, for
  149. * FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  150. * FIRDataEventTypeChildChanged 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. * FIRDataSnapshot 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 observeEventType:withBlock:, 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 at
  166. * 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. * FIRDataSnapshot.
  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 observeEventType:withBlock:, except the block is
  179. * immediately canceled after the initial data is returned. In addition, for
  180. * FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  181. * FIRDataEventTypeChildChanged 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 at
  185. * 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. * FIRDataSnapshot 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 observeEventType:withBlock:.
  201. *
  202. * @param handle The handle returned by the call to observeEventType:withBlock:
  203. * which we are trying to remove.
  204. */
  205. - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle;
  206. /**
  207. * Detach all blocks previously attached to this Firebase Database location with
  208. * observeEventType:withBlock:
  209. */
  210. - (void)removeAllObservers;
  211. /**
  212. * By calling `keepSynced:YES` on a location, the data for that location will
  213. * automatically be downloaded and kept in sync, even when no listeners are
  214. * attached for that location. Additionally, while a location is kept synced, it
  215. * will not be evicted from the persistent disk cache.
  216. *
  217. * @param keepSynced Pass YES to keep this location synchronized, pass NO to
  218. * stop synchronization.
  219. */
  220. - (void)keepSynced:(BOOL)keepSynced;
  221. #pragma mark - Querying and limiting
  222. /**
  223. * queryLimitedToFirst: is used to generate a reference to a limited view of the
  224. * data at this location. The FIRDatabaseQuery instance returned by
  225. * queryLimitedToFirst: will respond to at most the first limit child nodes.
  226. *
  227. * @param limit The upper bound, inclusive, for the number of child nodes to
  228. * receive events for
  229. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  230. */
  231. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit;
  232. /**
  233. * queryLimitedToLast: is used to generate a reference to a limited view of the
  234. * data at this location. The FIRDatabaseQuery instance returned by
  235. * queryLimitedToLast: will respond to at most the last limit child nodes.
  236. *
  237. * @param limit The upper bound, inclusive, for the number of child nodes to
  238. * receive events for
  239. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  240. */
  241. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit;
  242. /**
  243. * queryOrderBy: is used to generate a reference to a view of the data that's
  244. * been sorted by the values of a particular child key. This method is intended
  245. * to be used in combination with queryStartingAtValue:, queryEndingAtValue:, or
  246. * queryEqualToValue:.
  247. *
  248. * @param key The child key to use in ordering data visible to the returned
  249. * FIRDatabaseQuery
  250. * @return A FIRDatabaseQuery instance, ordered by the values of the specified
  251. * child key.
  252. */
  253. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key;
  254. /**
  255. * queryOrderedByKey: is used to generate a reference to a view of the data
  256. * that's been sorted by child key. This method is intended to be used in
  257. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  258. * queryEqualToValue:.
  259. *
  260. * @return A FIRDatabaseQuery instance, ordered by child keys.
  261. */
  262. - (FIRDatabaseQuery *)queryOrderedByKey;
  263. /**
  264. * queryOrderedByValue: is used to generate a reference to a view of the data
  265. * that's been sorted by child value. This method is intended to be used in
  266. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  267. * queryEqualToValue:.
  268. *
  269. * @return A FIRDatabaseQuery instance, ordered by child value.
  270. */
  271. - (FIRDatabaseQuery *)queryOrderedByValue;
  272. /**
  273. * queryOrderedByPriority: is used to generate a reference to a view of the data
  274. * that's been sorted by child priority. This method is intended to be used in
  275. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  276. * queryEqualToValue:.
  277. *
  278. * @return A FIRDatabaseQuery instance, ordered by child priorities.
  279. */
  280. - (FIRDatabaseQuery *)queryOrderedByPriority;
  281. /**
  282. * queryStartingAtValue: is used to generate a reference to a limited view of
  283. * the data at this location. The FIRDatabaseQuery instance returned by
  284. * queryStartingAtValue: will respond to events at nodes with a value greater
  285. * than or equal to startValue.
  286. *
  287. * @param startValue The lower bound, inclusive, for the value of data visible
  288. * to the returned FIRDatabaseQuery
  289. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  290. * or equal to startValue
  291. */
  292. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue;
  293. /**
  294. * queryStartingAtValue:childKey: is used to generate a reference to a limited
  295. * view of the data at this location. The FIRDatabaseQuery instance returned by
  296. * queryStartingAtValue:childKey will respond to events at nodes with a value
  297. * greater than startValue, or equal to startValue and with a key greater than
  298. * or equal to childKey. This is most useful when implementing pagination in a
  299. * case where multiple nodes can match the startValue.
  300. *
  301. * @param startValue The lower bound, inclusive, for the value of data visible
  302. * to the returned FIRDatabaseQuery
  303. * @param childKey The lower bound, inclusive, for the key of nodes with value
  304. * equal to startValue
  305. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  306. * or equal to startValue
  307. */
  308. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue
  309. childKey:(nullable NSString *)childKey;
  310. /**
  311. * queryStartingAfterValue: is used to generate a reference to a
  312. * limited view of the data at this location. The FIRDatabaseQuery instance
  313. * returned by queryStartingAfterValue: will respond to events at nodes
  314. * with a value greater than startAfterValue.
  315. *
  316. * @param startAfterValue The lower bound, exclusive, for the value of data
  317. * visible to the returned FIRDatabaseQuery
  318. * @return A FIRDatabaseQuery instance, limited to data with value greater
  319. * startAfterValue
  320. */
  321. - (FIRDatabaseQuery *)queryStartingAfterValue:(nullable id)startAfterValue;
  322. /**
  323. * queryStartingAfterValue:childKey: is used to generate a reference to a
  324. * limited view of the data at this location. The FIRDatabaseQuery instance
  325. * returned by queryStartingAfterValue:childKey will respond to events at nodes
  326. * with a value greater than startAfterValue, or equal to startAfterValue and
  327. * with a key greater than childKey. This is most useful when implementing
  328. * pagination in a case where multiple nodes can match the startAfterValue.
  329. *
  330. * @param startAfterValue The lower bound, inclusive, for the value of data
  331. * visible to the returned FIRDatabaseQuery
  332. * @param childKey The lower bound, exclusive, for the key of nodes with value
  333. * equal to startAfterValue
  334. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  335. * startAfterValue, or equal to startAfterValue with a key greater than childKey
  336. */
  337. - (FIRDatabaseQuery *)queryStartingAfterValue:(nullable id)startAfterValue
  338. childKey:(nullable NSString *)childKey;
  339. /**
  340. * queryEndingAtValue: is used to generate a reference to a limited view of the
  341. * data at this location. The FIRDatabaseQuery instance returned by
  342. * queryEndingAtValue: will respond to events at nodes with a value less than or
  343. * equal to endValue.
  344. *
  345. * @param endValue The upper bound, inclusive, for the value of data visible to
  346. * the returned FIRDatabaseQuery
  347. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  348. * equal to endValue
  349. */
  350. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue;
  351. /**
  352. * queryEndingAtValue:childKey: is used to generate a reference to a limited
  353. * view of the data at this location. The FIRDatabaseQuery instance returned by
  354. * queryEndingAtValue:childKey will respond to events at nodes with a value less
  355. * than endValue, or equal to endValue and with a key less than or equal to
  356. * childKey. This is most useful when implementing pagination in a case where
  357. * multiple nodes can match the endValue.
  358. *
  359. * @param endValue The upper bound, inclusive, for the value of data visible to
  360. * the returned FIRDatabaseQuery
  361. * @param childKey The upper bound, inclusive, for the key of nodes with value
  362. * equal to endValue
  363. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  364. * equal to endValue
  365. */
  366. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue
  367. childKey:(nullable NSString *)childKey;
  368. /**
  369. * queryEndingBeforeValue: is used to generate a reference to a limited view of
  370. * the data at this location. The FIRDatabaseQuery instance returned by
  371. * queryEndingBeforeValue: will respond to events at nodes with a value less
  372. * than endValue.
  373. *
  374. * @param endValue The upper bound, exclusive, for the value of data visible to
  375. * the returned FIRDatabaseQuery
  376. * @return A FIRDatabaseQuery instance, limited to data with value less than
  377. * endValue
  378. */
  379. - (FIRDatabaseQuery *)queryEndingBeforeValue:(nullable id)endValue;
  380. /**
  381. * queryEndingBeforeValue:childKey: is used to generate a reference to a limited
  382. * view of the data at this location. The FIRDatabaseQuery instance returned by
  383. * queryEndingBeforeValue:childKey will respond to events at nodes with a value
  384. * less than endValue, or equal to endValue and with a key less than childKey.
  385. *
  386. * @param endValue The upper bound, inclusive, for the value of data visible to
  387. * the returned FIRDatabaseQuery
  388. * @param childKey The upper bound, exclusive, for the key of nodes with value
  389. * equal to endValue
  390. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  391. * equal to endValue
  392. */
  393. - (FIRDatabaseQuery *)queryEndingBeforeValue:(nullable id)endValue
  394. childKey:(nullable NSString *)childKey;
  395. /**
  396. * queryEqualToValue: is used to generate a reference to a limited view of the
  397. * data at this location. The FIRDatabaseQuery instance returned by
  398. * queryEqualToValue: will respond to events at nodes with a value equal to the
  399. * supplied argument.
  400. *
  401. * @param value The value that the data returned by this FIRDatabaseQuery will
  402. * have
  403. * @return A FIRDatabaseQuery instance, limited to data with the supplied value.
  404. */
  405. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value;
  406. /**
  407. * queryEqualToValue:childKey: is used to generate a reference to a limited view
  408. * of the data at this location. The FIRDatabaseQuery instance returned by
  409. * queryEqualToValue:childKey will respond to events at nodes with a value equal
  410. * to the supplied argument and with their key equal to childKey. There will be
  411. * at most one node that matches because child keys are unique.
  412. *
  413. * @param value The value that the data returned by this FIRDatabaseQuery will
  414. * have
  415. * @param childKey The name of nodes with the right value
  416. * @return A FIRDatabaseQuery instance, limited to data with the supplied value
  417. * and the key.
  418. */
  419. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value
  420. childKey:(nullable NSString *)childKey;
  421. #pragma mark - Properties
  422. /**
  423. * Gets a FIRDatabaseReference for the location of this query.
  424. *
  425. * @return A FIRDatabaseReference for the location of this query.
  426. */
  427. @property(nonatomic, readonly, strong) FIRDatabaseReference *ref;
  428. @end
  429. NS_ASSUME_NONNULL_END