FIRDatabaseReference.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 "FIRDataSnapshot.h"
  17. #import "FIRDatabase.h"
  18. #import "FIRDatabaseQuery.h"
  19. #import "FIRMutableData.h"
  20. #import "FIRServerValue.h"
  21. #import "FIRTransactionResult.h"
  22. #import <Foundation/Foundation.h>
  23. NS_ASSUME_NONNULL_BEGIN
  24. @class FIRDatabase;
  25. /**
  26. * A FIRDatabaseReference represents a particular location in your Firebase
  27. * Database and can be used for reading or writing data to that Firebase
  28. * Database location.
  29. *
  30. * This class is the starting point for all Firebase Database operations. After
  31. * you've obtained your first FIRDatabaseReference via [FIRDatabase reference],
  32. * you can use it to read data (ie. observeEventType:withBlock:), write data
  33. * (ie. setValue:), and to create new FIRDatabaseReferences (ie. child:).
  34. */
  35. NS_SWIFT_NAME(DatabaseReference)
  36. @interface FIRDatabaseReference : FIRDatabaseQuery
  37. #pragma mark - Getting references to children locations
  38. /**
  39. * Gets a FIRDatabaseReference for the location at the specified relative path.
  40. * The relative path can either be a simple child key (e.g. 'fred') or a
  41. * deeper slash-separated path (e.g. 'fred/name/first').
  42. *
  43. * @param pathString A relative path from this location to the desired child
  44. * location.
  45. * @return A FIRDatabaseReference for the specified relative path.
  46. */
  47. - (FIRDatabaseReference *)child:(NSString *)pathString;
  48. /**
  49. * childByAutoId generates a new child location using a unique key and returns a
  50. * FIRDatabaseReference to it. This is useful when the children of a Firebase
  51. * Database location represent a list of items.
  52. *
  53. * The unique key generated by childByAutoId: is prefixed with a
  54. * client-generated timestamp so that the resulting list will be
  55. * chronologically-sorted.
  56. *
  57. * @return A FIRDatabaseReference for the generated location.
  58. */
  59. - (FIRDatabaseReference *)childByAutoId;
  60. #pragma mark - Writing data
  61. /** Write data to this Firebase Database location.
  62. This will overwrite any data at this location and all child locations.
  63. Data types that can be set are:
  64. - NSString -- @"Hello World"
  65. - NSNumber (also includes boolean) -- @YES, @43, @4.333
  66. - NSDictionary -- @{@"key": @"value", @"nested": @{@"another": @"value"} }
  67. - NSArray
  68. The effect of the write will be visible immediately and the corresponding
  69. events will be triggered. Synchronization of the data to the Firebase Database
  70. servers will also be started.
  71. Passing null for the new value is equivalent to calling remove:;
  72. all data at this location or any child location will be deleted.
  73. Note that setValue: will remove any priority stored at this location, so if
  74. priority is meant to be preserved, you should use setValue:andPriority: instead.
  75. @param value The value to be written.
  76. */
  77. - (void)setValue:(nullable id)value;
  78. /**
  79. * The same as setValue: with a block that gets triggered after the write
  80. * operation has been committed to the Firebase Database servers.
  81. *
  82. * @param value The value to be written.
  83. * @param block The block to be called after the write has been committed to the
  84. * Firebase Database servers.
  85. */
  86. - (void)setValue:(nullable id)value
  87. withCompletionBlock:
  88. (void (^)(NSError *__nullable error, FIRDatabaseReference *ref))block;
  89. /**
  90. * The same as setValue: with an additional priority to be attached to the data
  91. * being written. Priorities are used to order items.
  92. *
  93. * @param value The value to be written.
  94. * @param priority The priority to be attached to that data.
  95. */
  96. - (void)setValue:(nullable id)value andPriority:(nullable id)priority;
  97. /**
  98. * The same as setValue:andPriority: with a block that gets triggered after the
  99. * write operation has been committed to the Firebase Database servers.
  100. *
  101. * @param value The value to be written.
  102. * @param priority The priority to be attached to that data.
  103. * @param block The block to be called after the write has been committed to the
  104. * Firebase Database servers.
  105. */
  106. - (void)setValue:(nullable id)value
  107. andPriority:(nullable id)priority
  108. withCompletionBlock:
  109. (void (^)(NSError *__nullable error, FIRDatabaseReference *ref))block;
  110. /**
  111. * Remove the data at this Firebase Database location. Any data at child
  112. * locations will also be deleted.
  113. *
  114. * The effect of the delete will be visible immediately and the corresponding
  115. * events will be triggered. Synchronization of the delete to the Firebase
  116. * Database servers will also be started.
  117. *
  118. * remove: is equivalent to calling setValue:nil
  119. */
  120. - (void)removeValue;
  121. /**
  122. * The same as remove: with a block that gets triggered after the remove
  123. * operation has been committed to the Firebase Database servers.
  124. *
  125. * @param block The block to be called after the remove has been committed to
  126. * the Firebase Database servers.
  127. */
  128. - (void)removeValueWithCompletionBlock:
  129. (void (^)(NSError *__nullable error, FIRDatabaseReference *ref))block;
  130. /**
  131. * Sets a priority for the data at this Firebase Database location.
  132. * Priorities can be used to provide a custom ordering for the children at a
  133. * location (if no priorities are specified, the children are ordered by key).
  134. *
  135. * You cannot set a priority on an empty location. For this reason
  136. * setValue:andPriority: should be used when setting initial data with a
  137. * specific priority and setPriority: should be used when updating the priority
  138. * of existing data.
  139. *
  140. * Children are sorted based on this priority using the following rules:
  141. *
  142. * Children with no priority come first.
  143. * Children with a number as their priority come next. They are sorted
  144. * numerically by priority (small to large). Children with a string as their
  145. * priority come last. They are sorted lexicographically by priority. Whenever
  146. * two children have the same priority (including no priority), they are sorted
  147. * by key. Numeric keys come first (sorted numerically), followed by the
  148. * remaining keys (sorted lexicographically).
  149. *
  150. * Note that priorities are parsed and ordered as IEEE 754 double-precision
  151. * floating-point numbers. Keys are always stored as strings and are treated as
  152. * numbers only when they can be parsed as a 32-bit integer
  153. *
  154. * @param priority The priority to set at the specified location.
  155. */
  156. - (void)setPriority:(nullable id)priority;
  157. /**
  158. * The same as setPriority: with a block that is called once the priority has
  159. * been committed to the Firebase Database servers.
  160. *
  161. * @param priority The priority to set at the specified location.
  162. * @param block The block that is triggered after the priority has been written
  163. * on the servers.
  164. */
  165. - (void)setPriority:(nullable id)priority
  166. withCompletionBlock:
  167. (void (^)(NSError *__nullable error, FIRDatabaseReference *ref))block;
  168. /**
  169. * Updates the values at the specified paths in the dictionary without
  170. * overwriting other keys at this location.
  171. *
  172. * @param values A dictionary of the keys to change and their new values
  173. */
  174. - (void)updateChildValues:(NSDictionary *)values;
  175. /**
  176. * The same as update: with a block that is called once the update has been
  177. * committed to the Firebase Database servers
  178. *
  179. * @param values A dictionary of the keys to change and their new values
  180. * @param block The block that is triggered after the update has been written on
  181. * the Firebase Database servers
  182. */
  183. - (void)updateChildValues:(NSDictionary *)values
  184. withCompletionBlock:
  185. (void (^)(NSError *__nullable error, FIRDatabaseReference *ref))block;
  186. #pragma mark - Attaching observers to read data
  187. /**
  188. * observeEventType:withBlock: is used to listen for data changes at a
  189. * particular location. This is the primary way to read data from the Firebase
  190. * Database. Your block will be triggered for the initial data and again
  191. * whenever the data changes.
  192. *
  193. * Use removeObserverWithHandle: to stop receiving updates.
  194. * @param eventType The type of event to listen for.
  195. * @param block The block that should be called with initial data and updates.
  196. * It is passed the data as a FIRDataSnapshot.
  197. * @return A handle used to unregister this block later using
  198. * removeObserverWithHandle:
  199. */
  200. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  201. withBlock:
  202. (void (^)(FIRDataSnapshot *snapshot))block;
  203. /**
  204. * observeEventType:andPreviousSiblingKeyWithBlock: is used to listen for data
  205. * changes at a particular location. This is the primary way to read data from
  206. * the Firebase Database. Your block will be triggered for the initial data and
  207. * again whenever the data changes. In addition, for FIRDataEventTypeChildAdded,
  208. * FIRDataEventTypeChildMoved, and FIRDataEventTypeChildChanged events, your
  209. * block will be passed the key of the previous node by priority order.
  210. *
  211. * Use removeObserverWithHandle: to stop receiving updates.
  212. *
  213. * @param eventType The type of event to listen for.
  214. * @param block The block that should be called with initial data and updates.
  215. * It is passed the data as a FIRDataSnapshot and the previous child's key.
  216. * @return A handle used to unregister this block later using
  217. * removeObserverWithHandle:
  218. */
  219. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  220. andPreviousSiblingKeyWithBlock:
  221. (void (^)(FIRDataSnapshot *snapshot,
  222. NSString *__nullable prevKey))block;
  223. /**
  224. * observeEventType:withBlock: is used to listen for data changes at a
  225. * particular location. This is the primary way to read data from the Firebase
  226. * Database. Your block will be triggered for the initial data and again
  227. * whenever the data changes.
  228. *
  229. * The cancelBlock will be called if you will no longer receive new events due
  230. * to no longer having permission.
  231. *
  232. * Use removeObserverWithHandle: to stop receiving updates.
  233. *
  234. * @param eventType The type of event to listen for.
  235. * @param block The block that should be called with initial data and updates.
  236. * It is passed the data as a FIRDataSnapshot.
  237. * @param cancelBlock The block that should be called if this client no longer
  238. * has permission to receive these events
  239. * @return A handle used to unregister this block later using
  240. * removeObserverWithHandle:
  241. */
  242. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  243. withBlock:(void (^)(FIRDataSnapshot *snapshot))block
  244. withCancelBlock:
  245. (nullable void (^)(NSError *error))cancelBlock;
  246. /**
  247. * observeEventType:andPreviousSiblingKeyWithBlock: is used to listen for data
  248. * changes at a particular location. This is the primary way to read data from
  249. * the Firebase Database. Your block will be triggered for the initial data and
  250. * again whenever the data changes. In addition, for FIRDataEventTypeChildAdded,
  251. * FIRDataEventTypeChildMoved, and FIRDataEventTypeChildChanged events, your
  252. * block will be passed the key of the previous node by priority order.
  253. *
  254. * The cancelBlock will be called if you will no longer receive new events due
  255. * to no longer having permission.
  256. *
  257. * Use removeObserverWithHandle: to stop receiving updates.
  258. *
  259. * @param eventType The type of event to listen for.
  260. * @param block The block that should be called with initial data and updates.
  261. * It is passed the data as a FIRDataSnapshot and the previous child's key.
  262. * @param cancelBlock The block that should be called if this client no longer
  263. * has permission to receive these events
  264. * @return A handle used to unregister this block later using
  265. * removeObserverWithHandle:
  266. */
  267. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  268. andPreviousSiblingKeyWithBlock:
  269. (void (^)(FIRDataSnapshot *snapshot,
  270. NSString *__nullable prevKey))block
  271. withCancelBlock:
  272. (nullable void (^)(NSError *error))cancelBlock;
  273. /**
  274. * This is equivalent to observeEventType:withBlock:, except the block is
  275. * immediately canceled after the initial data is returned.
  276. *
  277. * @param eventType The type of event to listen for.
  278. * @param block The block that should be called. It is passed the data as a
  279. * FIRDataSnapshot.
  280. */
  281. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  282. withBlock:(void (^)(FIRDataSnapshot *snapshot))block;
  283. /**
  284. * This is equivalent to observeEventType:withBlock:, except the block is
  285. * immediately canceled after the initial data is returned. In addition, for
  286. * FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  287. * FIRDataEventTypeChildChanged events, your block will be passed the key of the
  288. * previous node by priority order.
  289. *
  290. * @param eventType The type of event to listen for.
  291. * @param block The block that should be called. It is passed the data as a
  292. * FIRDataSnapshot and the previous child's key.
  293. */
  294. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  295. andPreviousSiblingKeyWithBlock:
  296. (void (^)(FIRDataSnapshot *snapshot,
  297. NSString *__nullable prevKey))block;
  298. /**
  299. * This is equivalent to observeEventType:withBlock:, except the block is
  300. * immediately canceled after the initial data is returned.
  301. *
  302. * The cancelBlock will be called if you do not have permission to read data at
  303. * this location.
  304. *
  305. * @param eventType The type of event to listen for.
  306. * @param block The block that should be called. It is passed the data as a
  307. * FIRDataSnapshot.
  308. * @param cancelBlock The block that will be called if you don't have permission
  309. * to access this data
  310. */
  311. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  312. withBlock:(void (^)(FIRDataSnapshot *snapshot))block
  313. withCancelBlock:(nullable void (^)(NSError *error))cancelBlock;
  314. /**
  315. * This is equivalent to observeEventType:withBlock:, except the block is
  316. * immediately canceled after the initial data is returned. In addition, for
  317. * FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and
  318. * FIRDataEventTypeChildChanged events, your block will be passed the key of the
  319. * previous node by priority order.
  320. *
  321. * The cancelBlock will be called if you do not have permission to read data at
  322. * this location.
  323. *
  324. * @param eventType The type of event to listen for.
  325. * @param block The block that should be called. It is passed the data as a
  326. * FIRDataSnapshot and the previous child's key.
  327. * @param cancelBlock The block that will be called if you don't have permission
  328. * to access this data
  329. */
  330. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  331. andPreviousSiblingKeyWithBlock:(void (^)(FIRDataSnapshot *snapshot,
  332. NSString *__nullable prevKey))block
  333. withCancelBlock:
  334. (nullable void (^)(NSError *error))cancelBlock;
  335. /**
  336. * getDataWithCompletionBlock: is used to get the most up-to-date value for
  337. * this query. This method updates the cache and raises events if successful. If
  338. * not connected, falls back to a locally-cached value.
  339. *
  340. * @param block The block that should be called with the most up-to-date value
  341. * of this query, or an error if no such value could be retrieved.
  342. */
  343. - (void)getDataWithCompletionBlock:
  344. (void (^_Nonnull)(NSError *__nullable error,
  345. FIRDataSnapshot *snapshot))block
  346. NS_SWIFT_NAME(getData(completion:));
  347. #pragma mark - Detaching observers
  348. /**
  349. * Detach a block previously attached with observeEventType:withBlock:.
  350. *
  351. * @param handle The handle returned by the call to observeEventType:withBlock:
  352. * which we are trying to remove.
  353. */
  354. - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle;
  355. /**
  356. * By calling `keepSynced:YES` on a location, the data for that location will
  357. * automatically be downloaded and kept in sync, even when no listeners are
  358. * attached for that location. Additionally, while a location is kept synced, it
  359. * will not be evicted from the persistent disk cache.
  360. *
  361. * @param keepSynced Pass YES to keep this location synchronized, pass NO to
  362. * stop synchronization.
  363. */
  364. - (void)keepSynced:(BOOL)keepSynced;
  365. /**
  366. * Removes all observers at the current reference, but does not remove any
  367. * observers at child references. removeAllObservers must be called again for
  368. * each child reference where a listener was established to remove the
  369. * observers.
  370. */
  371. - (void)removeAllObservers;
  372. #pragma mark - Querying and limiting
  373. /**
  374. * queryLimitedToFirst: is used to generate a reference to a limited view of the
  375. * data at this location. The FIRDatabaseQuery instance returned by
  376. * queryLimitedToFirst: will respond to at most the first limit child nodes.
  377. *
  378. * @param limit The upper bound, inclusive, for the number of child nodes to
  379. * receive events for
  380. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  381. */
  382. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit;
  383. /**
  384. * queryLimitedToLast: is used to generate a reference to a limited view of the
  385. * data at this location. The FIRDatabaseQuery instance returned by
  386. * queryLimitedToLast: will respond to at most the last limit child nodes.
  387. *
  388. * @param limit The upper bound, inclusive, for the number of child nodes to
  389. * receive events for
  390. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  391. */
  392. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit;
  393. /**
  394. * queryOrderBy: is used to generate a reference to a view of the data that's
  395. * been sorted by the values of a particular child key. This method is intended
  396. * to be used in combination with queryStartingAtValue:, queryEndingAtValue:, or
  397. * queryEqualToValue:.
  398. *
  399. * @param key The child key to use in ordering data visible to the returned
  400. * FIRDatabaseQuery
  401. * @return A FIRDatabaseQuery instance, ordered by the values of the specified
  402. * child key.
  403. */
  404. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key;
  405. /**
  406. * queryOrderedByKey: is used to generate a reference to a view of the data
  407. * that's been sorted by child key. This method is intended to be used in
  408. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  409. * queryEqualToValue:.
  410. *
  411. * @return A FIRDatabaseQuery instance, ordered by child keys.
  412. */
  413. - (FIRDatabaseQuery *)queryOrderedByKey;
  414. /**
  415. * queryOrderedByPriority: is used to generate a reference to a view of the data
  416. * that's been sorted by child priority. This method is intended to be used in
  417. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  418. * queryEqualToValue:.
  419. *
  420. * @return A FIRDatabaseQuery instance, ordered by child priorities.
  421. */
  422. - (FIRDatabaseQuery *)queryOrderedByPriority;
  423. /**
  424. * queryStartingAtValue: is used to generate a reference to a limited view of
  425. * the data at this location. The FIRDatabaseQuery instance returned by
  426. * queryStartingAtValue: will respond to events at nodes with a value greater
  427. * than or equal to startValue.
  428. *
  429. * @param startValue The lower bound, inclusive, for the value of data visible
  430. * to the returned FIRDatabaseQuery
  431. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  432. * or equal to startValue
  433. */
  434. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue;
  435. /**
  436. * queryStartingAtValue:childKey: is used to generate a reference to a limited
  437. * view of the data at this location. The FIRDatabaseQuery instance returned by
  438. * queryStartingAtValue:childKey will respond to events at nodes with a value
  439. * greater than startValue, or equal to startValue and with a key greater than
  440. * or equal to childKey.
  441. *
  442. * @param startValue The lower bound, inclusive, for the value of data visible
  443. * to the returned FIRDatabaseQuery
  444. * @param childKey The lower bound, inclusive, for the key of nodes with value
  445. * equal to startValue
  446. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  447. * or equal to startValue
  448. */
  449. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue
  450. childKey:(nullable NSString *)childKey;
  451. /**
  452. * queryStartingAfterValue: is used to generate a reference to a limited view of
  453. * the data at this location. The FIRDatabaseQuery instance returned by
  454. * queryStartingAfterValue: will respond to events at nodes with a value greater
  455. * than startAfterValue.
  456. *
  457. * @param startAfterValue The lower bound, exclusive, for the value of data
  458. * visible to the returned FIRDatabaseQuery
  459. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  460. * startAfterValue
  461. */
  462. - (FIRDatabaseQuery *)queryStartingAfterValue:(nullable id)startAfterValue;
  463. /**
  464. * queryStartingAfterValue:childKey: is used to generate a reference to a
  465. * limited view of the data at this location. The FIRDatabaseQuery instance
  466. * returned by queryStartingAfterValue:childKey will respond to events at nodes
  467. * with a value greater than startAfterValue, or equal to startAfterValue and
  468. * with a key greater than childKey. This is most useful when implementing
  469. * pagination in a case where multiple nodes can match the startAfterValue.
  470. *
  471. * @param startAfterValue The lower bound, inclusive, for the value of data
  472. * visible to the returned FIRDatabaseQuery
  473. * @param childKey The lower bound, exclusive, for the key of nodes with value
  474. * equal to startAfterValue
  475. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  476. * or equal to startAfterValue, or equal to startAfterValue and with a key
  477. * greater than childKey.
  478. */
  479. - (FIRDatabaseQuery *)queryStartingAfterValue:(nullable id)startAfterValue
  480. childKey:(nullable NSString *)childKey;
  481. /**
  482. * queryEndingAtValue: is used to generate a reference to a limited view of the
  483. * data at this location. The FIRDatabaseQuery instance returned by
  484. * queryEndingAtValue: will respond to events at nodes with a value less than or
  485. * equal to endValue.
  486. *
  487. * @param endValue The upper bound, inclusive, for the value of data visible to
  488. * the returned FIRDatabaseQuery
  489. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  490. * equal to endValue
  491. */
  492. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue;
  493. /**
  494. * queryEndingAtValue:childKey: is used to generate a reference to a limited
  495. * view of the data at this location. The FIRDatabaseQuery instance returned by
  496. * queryEndingAtValue:childKey will respond to events at nodes with a value less
  497. * than endValue, or equal to endValue and with a key less than or equal to
  498. * childKey.
  499. *
  500. * @param endValue The upper bound, inclusive, for the value of data visible to
  501. * the returned FIRDatabaseQuery
  502. * @param childKey The upper bound, inclusive, for the key of nodes with value
  503. * equal to endValue
  504. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  505. * equal to endValue
  506. */
  507. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue
  508. childKey:(nullable NSString *)childKey;
  509. /**
  510. * queryEqualToValue: is used to generate a reference to a limited view of the
  511. * data at this location. The FIRDatabaseQuery instance returned by
  512. * queryEqualToValue: will respond to events at nodes with a value equal to the
  513. * supplied argument.
  514. *
  515. * @param value The value that the data returned by this FIRDatabaseQuery will
  516. * have
  517. * @return A FIRDatabaseQuery instance, limited to data with the supplied value.
  518. */
  519. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value;
  520. /**
  521. * queryEqualToValue:childKey: is used to generate a reference to a limited view
  522. * of the data at this location. The FIRDatabaseQuery instance returned by
  523. * queryEqualToValue:childKey will respond to events at nodes with a value equal
  524. * to the supplied argument with a key equal to childKey. There will be at most
  525. * one node that matches because child keys are unique.
  526. *
  527. * @param value The value that the data returned by this FIRDatabaseQuery will
  528. * have
  529. * @param childKey The key of nodes with the right value
  530. * @return A FIRDatabaseQuery instance, limited to data with the supplied value
  531. * and the key.
  532. */
  533. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value
  534. childKey:(nullable NSString *)childKey;
  535. #pragma mark - Managing presence
  536. /**
  537. * Ensure the data at this location is set to the specified value when
  538. * the client is disconnected (due to closing the browser, navigating
  539. * to a new page, or network issues).
  540. *
  541. * onDisconnectSetValue: is especially useful for implementing "presence"
  542. * systems, where a value should be changed or cleared when a user disconnects
  543. * so that he appears "offline" to other users.
  544. *
  545. * @param value The value to be set after the connection is lost.
  546. */
  547. - (void)onDisconnectSetValue:(nullable id)value;
  548. /**
  549. * Ensure the data at this location is set to the specified value when
  550. * the client is disconnected (due to closing the browser, navigating
  551. * to a new page, or network issues).
  552. *
  553. * The completion block will be triggered when the operation has been
  554. * successfully queued up on the Firebase Database servers
  555. *
  556. * @param value The value to be set after the connection is lost.
  557. * @param block Block to be triggered when the operation has been queued up on
  558. * the Firebase Database servers
  559. */
  560. - (void)onDisconnectSetValue:(nullable id)value
  561. withCompletionBlock:(void (^)(NSError *__nullable error,
  562. FIRDatabaseReference *ref))block;
  563. /**
  564. * Ensure the data at this location is set to the specified value and priority
  565. * when the client is disconnected (due to closing the browser, navigating to a
  566. * new page, or network issues).
  567. *
  568. * @param value The value to be set after the connection is lost.
  569. * @param priority The priority to be set after the connection is lost.
  570. */
  571. - (void)onDisconnectSetValue:(nullable id)value andPriority:(id)priority;
  572. /**
  573. * Ensure the data at this location is set to the specified value and priority
  574. * when the client is disconnected (due to closing the browser, navigating to a
  575. * new page, or network issues).
  576. *
  577. * The completion block will be triggered when the operation has been
  578. * successfully queued up on the Firebase Database servers
  579. *
  580. * @param value The value to be set after the connection is lost.
  581. * @param priority The priority to be set after the connection is lost.
  582. * @param block Block to be triggered when the operation has been queued up on
  583. * the Firebase Database servers
  584. */
  585. - (void)onDisconnectSetValue:(nullable id)value
  586. andPriority:(nullable id)priority
  587. withCompletionBlock:(void (^)(NSError *__nullable error,
  588. FIRDatabaseReference *ref))block;
  589. /**
  590. * Ensure the data at this location is removed when
  591. * the client is disconnected (due to closing the app, navigating
  592. * to a new page, or network issues).
  593. *
  594. * onDisconnectRemoveValue is especially useful for implementing "presence"
  595. * systems.
  596. */
  597. - (void)onDisconnectRemoveValue;
  598. /**
  599. * Ensure the data at this location is removed when
  600. * the client is disconnected (due to closing the app, navigating
  601. * to a new page, or network issues).
  602. *
  603. * onDisconnectRemoveValueWithCompletionBlock: is especially useful for
  604. * implementing "presence" systems.
  605. *
  606. * @param block Block to be triggered when the operation has been queued up on
  607. * the Firebase Database servers
  608. */
  609. - (void)onDisconnectRemoveValueWithCompletionBlock:
  610. (void (^)(NSError *__nullable error, FIRDatabaseReference *ref))block;
  611. /**
  612. * Ensure the data has the specified child values updated when
  613. * the client is disconnected (due to closing the browser, navigating
  614. * to a new page, or network issues).
  615. *
  616. *
  617. * @param values A dictionary of child node keys and the values to set them to
  618. * after the connection is lost.
  619. */
  620. - (void)onDisconnectUpdateChildValues:(NSDictionary *)values;
  621. /**
  622. * Ensure the data has the specified child values updated when
  623. * the client is disconnected (due to closing the browser, navigating
  624. * to a new page, or network issues).
  625. *
  626. *
  627. * @param values A dictionary of child node keys and the values to set them to
  628. * after the connection is lost.
  629. * @param block A block that will be called once the operation has been queued
  630. * up on the Firebase Database servers
  631. */
  632. - (void)onDisconnectUpdateChildValues:(NSDictionary *)values
  633. withCompletionBlock:
  634. (void (^)(NSError *__nullable error,
  635. FIRDatabaseReference *ref))block;
  636. /**
  637. * Cancel any operations that are set to run on disconnect. If you previously
  638. * called onDisconnectSetValue:, onDisconnectRemoveValue:, or
  639. * onDisconnectUpdateChildValues:, and no longer want the values updated when
  640. * the connection is lost, call cancelDisconnectOperations:
  641. */
  642. - (void)cancelDisconnectOperations;
  643. /**
  644. * Cancel any operations that are set to run on disconnect. If you previously
  645. * called onDisconnectSetValue:, onDisconnectRemoveValue:, or
  646. * onDisconnectUpdateChildValues:, and no longer want the values updated when
  647. * the connection is lost, call cancelDisconnectOperations:
  648. *
  649. * @param block A block that will be triggered once the Firebase Database
  650. * servers have acknowledged the cancel request.
  651. */
  652. - (void)cancelDisconnectOperationsWithCompletionBlock:
  653. (nullable void (^)(NSError *__nullable error,
  654. FIRDatabaseReference *ref))block;
  655. #pragma mark - Manual Connection Management
  656. /**
  657. * Manually disconnect the Firebase Database client from the server and disable
  658. * automatic reconnection.
  659. *
  660. * The Firebase Database client automatically maintains a persistent connection
  661. * to the Firebase Database server, which will remain active indefinitely and
  662. * reconnect when disconnected. However, the goOffline( ) and goOnline( )
  663. * methods may be used to manually control the client connection in cases where
  664. * a persistent connection is undesirable.
  665. *
  666. * While offline, the Firebase Database client will no longer receive data
  667. * updates from the server. However, all database operations performed locally
  668. * will continue to immediately fire events, allowing your application to
  669. * continue behaving normally. Additionally, each operation performed locally
  670. * will automatically be queued and retried upon reconnection to the Firebase
  671. * Database server.
  672. *
  673. * To reconnect to the Firebase Database server and begin receiving remote
  674. * events, see goOnline( ). Once the connection is reestablished, the Firebase
  675. * Database client will transmit the appropriate data and fire the appropriate
  676. * events so that your client "catches up" automatically.
  677. *
  678. * Note: Invoking this method will impact all Firebase Database connections.
  679. */
  680. + (void)goOffline;
  681. /**
  682. * Manually reestablish a connection to the Firebase Database server and enable
  683. * automatic reconnection.
  684. *
  685. * The Firebase Database client automatically maintains a persistent connection
  686. * to the Firebase Database server, which will remain active indefinitely and
  687. * reconnect when disconnected. However, the goOffline( ) and goOnline( )
  688. * methods may be used to manually control the client connection in cases where
  689. * a persistent connection is undesirable.
  690. *
  691. * This method should be used after invoking goOffline( ) to disable the active
  692. * connection. Once reconnected, the Firebase Database client will automatically
  693. * transmit the proper data and fire the appropriate events so that your client
  694. * "catches up" automatically.
  695. *
  696. * To disconnect from the Firebase Database server, see goOffline( ).
  697. *
  698. * Note: Invoking this method will impact all Firebase Database connections.
  699. */
  700. + (void)goOnline;
  701. #pragma mark - Transactions
  702. /**
  703. * Performs an optimistic-concurrency transactional update to the data at this
  704. * location. Your block will be called with a FIRMutableData instance that
  705. * contains the current data at this location. Your block should update this
  706. * data to the value you wish to write to this location, and then return an
  707. * instance of FIRTransactionResult with the new data.
  708. *
  709. * If, when the operation reaches the server, it turns out that this client had
  710. * stale data, your block will be run again with the latest data from the
  711. * server.
  712. *
  713. * When your block is run, you may decide to abort the transaction by returning
  714. * [FIRTransactionResult abort].
  715. *
  716. * @param block This block receives the current data at this location and must
  717. * return an instance of FIRTransactionResult
  718. */
  719. - (void)runTransactionBlock:
  720. (FIRTransactionResult * (^)(FIRMutableData *currentData))block;
  721. /**
  722. * Performs an optimistic-concurrency transactional update to the data at this
  723. * location. Your block will be called with a FIRMutableData instance that
  724. * contains the current data at this location. Your block should update this
  725. * data to the value you wish to write to this location, and then return an
  726. * instance of FIRTransactionResult with the new data.
  727. *
  728. * If, when the operation reaches the server, it turns out that this client had
  729. * stale data, your block will be run again with the latest data from the
  730. * server.
  731. *
  732. * When your block is run, you may decide to abort the transaction by returning
  733. * [FIRTransactionResult abort].
  734. *
  735. * @param block This block receives the current data at this location and must
  736. * return an instance of FIRTransactionResult
  737. * @param completionBlock This block will be triggered once the transaction is
  738. * complete, whether it was successful or not. It will indicate if there was an
  739. * error, whether or not the data was committed, and what the current value of
  740. * the data at this location is.
  741. */
  742. - (void)runTransactionBlock:
  743. (FIRTransactionResult * (^)(FIRMutableData *currentData))block
  744. andCompletionBlock:
  745. (void (^)(NSError *__nullable error, BOOL committed,
  746. FIRDataSnapshot *__nullable snapshot))completionBlock;
  747. /**
  748. * Performs an optimistic-concurrency transactional update to the data at this
  749. * location. Your block will be called with a FIRMutableData instance that
  750. * contains the current data at this location. Your block should update this
  751. * data to the value you wish to write to this location, and then return an
  752. * instance of FIRTransactionResult with the new data.
  753. *
  754. * If, when the operation reaches the server, it turns out that this client had
  755. * stale data, your block will be run again with the latest data from the
  756. * server.
  757. *
  758. * When your block is run, you may decide to abort the transaction by return
  759. * [FIRTransactionResult abort].
  760. *
  761. * Since your block may be run multiple times, this client could see several
  762. * immediate states that don't exist on the server. You can suppress those
  763. * immediate states until the server confirms the final state of the
  764. * transaction.
  765. *
  766. * @param block This block receives the current data at this location and must
  767. * return an instance of FIRTransactionResult
  768. * @param completionBlock This block will be triggered once the transaction is
  769. * complete, whether it was successful or not. It will indicate if there was an
  770. * error, whether or not the data was committed, and what the current value of
  771. * the data at this location is.
  772. * @param localEvents Set this to NO to suppress events raised for intermediate
  773. * states, and only get events based on the final state of the transaction.
  774. */
  775. - (void)runTransactionBlock:
  776. (FIRTransactionResult * (^)(FIRMutableData *currentData))block
  777. andCompletionBlock:
  778. (nullable void (^)(NSError *__nullable error, BOOL committed,
  779. FIRDataSnapshot *__nullable snapshot))
  780. completionBlock
  781. withLocalEvents:(BOOL)localEvents;
  782. #pragma mark - Retrieving String Representation
  783. /**
  784. * Gets the absolute URL of this Firebase Database location.
  785. *
  786. * @return The absolute URL of the referenced Firebase Database location.
  787. */
  788. - (NSString *)description;
  789. #pragma mark - Properties
  790. /**
  791. * Gets a FIRDatabaseReference for the parent location.
  792. * If this instance refers to the root of your Firebase Database, it has no
  793. * parent, and therefore parent( ) will return null.
  794. *
  795. * @return A FIRDatabaseReference for the parent location.
  796. */
  797. @property(strong, readonly, nonatomic, nullable) FIRDatabaseReference *parent;
  798. /**
  799. * Gets a FIRDatabaseReference for the root location
  800. *
  801. * @return A new FIRDatabaseReference to root location.
  802. */
  803. @property(strong, readonly, nonatomic) FIRDatabaseReference *root;
  804. /**
  805. * Gets the last token in a Firebase Database location (e.g. 'fred' in
  806. * https&#58;//SampleChat.firebaseIO-demo.com/users/fred)
  807. *
  808. * @return The key of the location this reference points to.
  809. */
  810. @property(strong, readonly, nonatomic, nullable) NSString *key;
  811. /**
  812. * Gets the URL for the Firebase Database location referenced by this
  813. * FIRDatabaseReference.
  814. *
  815. * @return The url of the location this reference points to.
  816. */
  817. @property(strong, readonly, nonatomic) NSString *URL;
  818. /**
  819. * Gets the FIRDatabase instance associated with this reference.
  820. *
  821. * @return The FIRDatabase object for this reference.
  822. */
  823. @property(strong, readonly, nonatomic) FIRDatabase *database;
  824. @end
  825. NS_ASSUME_NONNULL_END