FIRDatabaseQuery.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. * This is equivalent to observeEventType:withBlock:, except the block is
  126. * immediately canceled after the initial data is returned.
  127. *
  128. * @param eventType The type of event to listen for.
  129. * @param block The block that should be called. It is passed the data as a
  130. * FIRDataSnapshot.
  131. */
  132. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  133. withBlock:(void (^)(FIRDataSnapshot *snapshot))block;
  134. /**
  135. * This is equivalent to observeEventType:withBlock:, except the block is
  136. * immediately canceled after the initial data is returned. In addition, for
  137. * FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  138. * FIRDataEventTypeChildChanged events, your block will be passed the key of the
  139. * previous node by priority order.
  140. *
  141. * @param eventType The type of event to listen for.
  142. * @param block The block that should be called. It is passed the data as a
  143. * FIRDataSnapshot and the previous child's key.
  144. */
  145. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  146. andPreviousSiblingKeyWithBlock:
  147. (void (^)(FIRDataSnapshot *snapshot,
  148. NSString *__nullable prevKey))block;
  149. /**
  150. * This is equivalent to observeEventType:withBlock:, except the block is
  151. * immediately canceled after the initial data is returned.
  152. *
  153. * The cancelBlock will be called if you do not have permission to read data at
  154. * this location.
  155. *
  156. * @param eventType The type of event to listen for.
  157. * @param block The block that should be called. It is passed the data as a
  158. * FIRDataSnapshot.
  159. * @param cancelBlock The block that will be called if you don't have permission
  160. * to access this data
  161. */
  162. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  163. withBlock:(void (^)(FIRDataSnapshot *snapshot))block
  164. withCancelBlock:(nullable void (^)(NSError *error))cancelBlock;
  165. /**
  166. * This is equivalent to observeEventType:withBlock:, except the block is
  167. * immediately canceled after the initial data is returned. In addition, for
  168. * FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  169. * FIRDataEventTypeChildChanged events, your block will be passed the key of the
  170. * previous node by priority order.
  171. *
  172. * The cancelBlock will be called if you do not have permission to read data at
  173. * this location.
  174. *
  175. * @param eventType The type of event to listen for.
  176. * @param block The block that should be called. It is passed the data as a
  177. * FIRDataSnapshot and the previous child's key.
  178. * @param cancelBlock The block that will be called if you don't have permission
  179. * to access this data
  180. */
  181. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  182. andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot *snapshot,
  183. NSString *__nullable prevKey))block
  184. withCancelBlock:
  185. (nullable void (^)(NSError *error))cancelBlock;
  186. #pragma mark - Detaching observers
  187. /**
  188. * Detach a block previously attached with observeEventType:withBlock:.
  189. *
  190. * @param handle The handle returned by the call to observeEventType:withBlock:
  191. * which we are trying to remove.
  192. */
  193. - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle;
  194. /**
  195. * Detach all blocks previously attached to this Firebase Database location with
  196. * observeEventType:withBlock:
  197. */
  198. - (void)removeAllObservers;
  199. /**
  200. * By calling `keepSynced:YES` on a location, the data for that location will
  201. * automatically be downloaded and kept in sync, even when no listeners are
  202. * attached for that location. Additionally, while a location is kept synced, it
  203. * will not be evicted from the persistent disk cache.
  204. *
  205. * @param keepSynced Pass YES to keep this location synchronized, pass NO to
  206. * stop synchronization.
  207. */
  208. - (void)keepSynced:(BOOL)keepSynced;
  209. #pragma mark - Querying and limiting
  210. /**
  211. * queryLimitedToFirst: is used to generate a reference to a limited view of the
  212. * data at this location. The FIRDatabaseQuery instance returned by
  213. * queryLimitedToFirst: will respond to at most the first limit child nodes.
  214. *
  215. * @param limit The upper bound, inclusive, for the number of child nodes to
  216. * receive events for
  217. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  218. */
  219. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit;
  220. /**
  221. * queryLimitedToLast: is used to generate a reference to a limited view of the
  222. * data at this location. The FIRDatabaseQuery instance returned by
  223. * queryLimitedToLast: will respond to at most the last limit child nodes.
  224. *
  225. * @param limit The upper bound, inclusive, for the number of child nodes to
  226. * receive events for
  227. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  228. */
  229. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit;
  230. /**
  231. * queryOrderBy: is used to generate a reference to a view of the data that's
  232. * been sorted by the values of a particular child key. This method is intended
  233. * to be used in combination with queryStartingAtValue:, queryEndingAtValue:, or
  234. * queryEqualToValue:.
  235. *
  236. * @param key The child key to use in ordering data visible to the returned
  237. * FIRDatabaseQuery
  238. * @return A FIRDatabaseQuery instance, ordered by the values of the specified
  239. * child key.
  240. */
  241. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key;
  242. /**
  243. * queryOrderedByKey: is used to generate a reference to a view of the data
  244. * that's been sorted by child key. This method is intended to be used in
  245. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  246. * queryEqualToValue:.
  247. *
  248. * @return A FIRDatabaseQuery instance, ordered by child keys.
  249. */
  250. - (FIRDatabaseQuery *)queryOrderedByKey;
  251. /**
  252. * queryOrderedByValue: is used to generate a reference to a view of the data
  253. * that's been sorted by child value. This method is intended to be used in
  254. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  255. * queryEqualToValue:.
  256. *
  257. * @return A FIRDatabaseQuery instance, ordered by child value.
  258. */
  259. - (FIRDatabaseQuery *)queryOrderedByValue;
  260. /**
  261. * queryOrderedByPriority: is used to generate a reference to a view of the data
  262. * that's been sorted by child priority. This method is intended to be used in
  263. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  264. * queryEqualToValue:.
  265. *
  266. * @return A FIRDatabaseQuery instance, ordered by child priorities.
  267. */
  268. - (FIRDatabaseQuery *)queryOrderedByPriority;
  269. /**
  270. * queryStartingAtValue: is used to generate a reference to a limited view of
  271. * the data at this location. The FIRDatabaseQuery instance returned by
  272. * queryStartingAtValue: will respond to events at nodes with a value greater
  273. * than or equal to startValue.
  274. *
  275. * @param startValue The lower bound, inclusive, for the value of data visible
  276. * to the returned FIRDatabaseQuery
  277. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  278. * or equal to startValue
  279. */
  280. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue;
  281. /**
  282. * queryStartingAtValue:childKey: is used to generate a reference to a limited
  283. * view of the data at this location. The FIRDatabaseQuery instance returned by
  284. * queryStartingAtValue:childKey will respond to events at nodes with a value
  285. * greater than startValue, or equal to startValue and with a key greater than
  286. * or equal to childKey. This is most useful when implementing pagination in a
  287. * case where multiple nodes can match the startValue.
  288. *
  289. * @param startValue The lower bound, inclusive, for the value of data visible
  290. * to the returned FIRDatabaseQuery
  291. * @param childKey The lower bound, inclusive, for the key of nodes with value
  292. * equal to startValue
  293. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  294. * or equal to startValue
  295. */
  296. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue
  297. childKey:(nullable NSString *)childKey;
  298. /**
  299. * queryEndingAtValue: is used to generate a reference to a limited view of the
  300. * data at this location. The FIRDatabaseQuery instance returned by
  301. * queryEndingAtValue: will respond to events at nodes with a value less than or
  302. * equal to endValue.
  303. *
  304. * @param endValue The upper bound, inclusive, for the value of data visible to
  305. * the returned FIRDatabaseQuery
  306. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  307. * equal to endValue
  308. */
  309. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue;
  310. /**
  311. * queryEndingAtValue:childKey: is used to generate a reference to a limited
  312. * view of the data at this location. The FIRDatabaseQuery instance returned by
  313. * queryEndingAtValue:childKey will respond to events at nodes with a value less
  314. * than endValue, or equal to endValue and with a key less than or equal to
  315. * childKey. This is most useful when implementing pagination in a case where
  316. * multiple nodes can match the endValue.
  317. *
  318. * @param endValue The upper bound, inclusive, for the value of data visible to
  319. * the returned FIRDatabaseQuery
  320. * @param childKey The upper bound, inclusive, for the key of nodes with value
  321. * equal to endValue
  322. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  323. * equal to endValue
  324. */
  325. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue
  326. childKey:(nullable NSString *)childKey;
  327. /**
  328. * queryEqualToValue: is used to generate a reference to a limited view of the
  329. * data at this location. The FIRDatabaseQuery instance returned by
  330. * queryEqualToValue: will respond to events at nodes with a value equal to the
  331. * supplied argument.
  332. *
  333. * @param value The value that the data returned by this FIRDatabaseQuery will
  334. * have
  335. * @return A FIRDatabaseQuery instance, limited to data with the supplied value.
  336. */
  337. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value;
  338. /**
  339. * queryEqualToValue:childKey: is used to generate a reference to a limited view
  340. * of the data at this location. The FIRDatabaseQuery instance returned by
  341. * queryEqualToValue:childKey will respond to events at nodes with a value equal
  342. * to the supplied argument and with their key equal to childKey. There will be
  343. * at most one node that matches because child keys are unique.
  344. *
  345. * @param value The value that the data returned by this FIRDatabaseQuery will
  346. * have
  347. * @param childKey The name of nodes with the right value
  348. * @return A FIRDatabaseQuery instance, limited to data with the supplied value
  349. * and the key.
  350. */
  351. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value
  352. childKey:(nullable NSString *)childKey;
  353. #pragma mark - Properties
  354. /**
  355. * Gets a FIRDatabaseReference for the location of this query.
  356. *
  357. * @return A FIRDatabaseReference for the location of this query.
  358. */
  359. @property(nonatomic, readonly, strong) FIRDatabaseReference *ref;
  360. @end
  361. NS_ASSUME_NONNULL_END