FIRDatabaseReference.h 37 KB

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