FIRDatabaseReference.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. #pragma mark - Detaching observers
  336. /**
  337. * Detach a block previously attached with observeEventType:withBlock:.
  338. *
  339. * @param handle The handle returned by the call to observeEventType:withBlock:
  340. * which we are trying to remove.
  341. */
  342. - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle;
  343. /**
  344. * By calling `keepSynced:YES` on a location, the data for that location will
  345. * automatically be downloaded and kept in sync, even when no listeners are
  346. * attached for that location. Additionally, while a location is kept synced, it
  347. * will not be evicted from the persistent disk cache.
  348. *
  349. * @param keepSynced Pass YES to keep this location synchronized, pass NO to
  350. * stop synchronization.
  351. */
  352. - (void)keepSynced:(BOOL)keepSynced;
  353. /**
  354. * Removes all observers at the current reference, but does not remove any
  355. * observers at child references. removeAllObservers must be called again for
  356. * each child reference where a listener was established to remove the
  357. * observers.
  358. */
  359. - (void)removeAllObservers;
  360. #pragma mark - Querying and limiting
  361. /**
  362. * queryLimitedToFirst: is used to generate a reference to a limited view of the
  363. * data at this location. The FIRDatabaseQuery instance returned by
  364. * queryLimitedToFirst: will respond to at most the first limit child nodes.
  365. *
  366. * @param limit The upper bound, inclusive, for the number of child nodes to
  367. * receive events for
  368. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  369. */
  370. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit;
  371. /**
  372. * queryLimitedToLast: is used to generate a reference to a limited view of the
  373. * data at this location. The FIRDatabaseQuery instance returned by
  374. * queryLimitedToLast: will respond to at most the last limit child nodes.
  375. *
  376. * @param limit The upper bound, inclusive, for the number of child nodes to
  377. * receive events for
  378. * @return A FIRDatabaseQuery instance, limited to at most limit child nodes.
  379. */
  380. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit;
  381. /**
  382. * queryOrderBy: is used to generate a reference to a view of the data that's
  383. * been sorted by the values of a particular child key. This method is intended
  384. * to be used in combination with queryStartingAtValue:, queryEndingAtValue:, or
  385. * queryEqualToValue:.
  386. *
  387. * @param key The child key to use in ordering data visible to the returned
  388. * FIRDatabaseQuery
  389. * @return A FIRDatabaseQuery instance, ordered by the values of the specified
  390. * child key.
  391. */
  392. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key;
  393. /**
  394. * queryOrderedByKey: is used to generate a reference to a view of the data
  395. * that's been sorted by child key. This method is intended to be used in
  396. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  397. * queryEqualToValue:.
  398. *
  399. * @return A FIRDatabaseQuery instance, ordered by child keys.
  400. */
  401. - (FIRDatabaseQuery *)queryOrderedByKey;
  402. /**
  403. * queryOrderedByPriority: is used to generate a reference to a view of the data
  404. * that's been sorted by child priority. This method is intended to be used in
  405. * combination with queryStartingAtValue:, queryEndingAtValue:, or
  406. * queryEqualToValue:.
  407. *
  408. * @return A FIRDatabaseQuery instance, ordered by child priorities.
  409. */
  410. - (FIRDatabaseQuery *)queryOrderedByPriority;
  411. /**
  412. * queryStartingAtValue: is used to generate a reference to a limited view of
  413. * the data at this location. The FIRDatabaseQuery instance returned by
  414. * queryStartingAtValue: will respond to events at nodes with a value greater
  415. * than or equal to startValue.
  416. *
  417. * @param startValue The lower bound, inclusive, for the value of data visible
  418. * to the returned FIRDatabaseQuery
  419. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  420. * or equal to startValue
  421. */
  422. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue;
  423. /**
  424. * queryStartingAtValue:childKey: is used to generate a reference to a limited
  425. * view of the data at this location. The FIRDatabaseQuery instance returned by
  426. * queryStartingAtValue:childKey will respond to events at nodes with a value
  427. * greater than startValue, or equal to startValue and with a key greater than
  428. * or equal to childKey.
  429. *
  430. * @param startValue The lower bound, inclusive, for the value of data visible
  431. * to the returned FIRDatabaseQuery
  432. * @param childKey The lower bound, inclusive, for the key of nodes with value
  433. * equal to startValue
  434. * @return A FIRDatabaseQuery instance, limited to data with value greater than
  435. * or equal to startValue
  436. */
  437. - (FIRDatabaseQuery *)queryStartingAtValue:(nullable id)startValue
  438. childKey:(nullable NSString *)childKey;
  439. /**
  440. * queryEndingAtValue: is used to generate a reference to a limited view of the
  441. * data at this location. The FIRDatabaseQuery instance returned by
  442. * queryEndingAtValue: will respond to events at nodes with a value less than or
  443. * equal to endValue.
  444. *
  445. * @param endValue The upper bound, inclusive, for the value of data visible to
  446. * the returned FIRDatabaseQuery
  447. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  448. * equal to endValue
  449. */
  450. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue;
  451. /**
  452. * queryEndingAtValue:childKey: is used to generate a reference to a limited
  453. * view of the data at this location. The FIRDatabaseQuery instance returned by
  454. * queryEndingAtValue:childKey will respond to events at nodes with a value less
  455. * than endValue, or equal to endValue and with a key less than or equal to
  456. * childKey.
  457. *
  458. * @param endValue The upper bound, inclusive, for the value of data visible to
  459. * the returned FIRDatabaseQuery
  460. * @param childKey The upper bound, inclusive, for the key of nodes with value
  461. * equal to endValue
  462. * @return A FIRDatabaseQuery instance, limited to data with value less than or
  463. * equal to endValue
  464. */
  465. - (FIRDatabaseQuery *)queryEndingAtValue:(nullable id)endValue
  466. childKey:(nullable NSString *)childKey;
  467. /**
  468. * queryEqualToValue: is used to generate a reference to a limited view of the
  469. * data at this location. The FIRDatabaseQuery instance returned by
  470. * queryEqualToValue: will respond to events at nodes with a value equal to the
  471. * supplied argument.
  472. *
  473. * @param value The value that the data returned by this FIRDatabaseQuery will
  474. * have
  475. * @return A FIRDatabaseQuery instance, limited to data with the supplied value.
  476. */
  477. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value;
  478. /**
  479. * queryEqualToValue:childKey: is used to generate a reference to a limited view
  480. * of the data at this location. The FIRDatabaseQuery instance returned by
  481. * queryEqualToValue:childKey will respond to events at nodes with a value equal
  482. * to the supplied argument with a key equal to childKey. There will be at most
  483. * one node that matches because child keys are unique.
  484. *
  485. * @param value The value that the data returned by this FIRDatabaseQuery will
  486. * have
  487. * @param childKey The key of nodes with the right value
  488. * @return A FIRDatabaseQuery instance, limited to data with the supplied value
  489. * and the key.
  490. */
  491. - (FIRDatabaseQuery *)queryEqualToValue:(nullable id)value
  492. childKey:(nullable NSString *)childKey;
  493. #pragma mark - Managing presence
  494. /**
  495. * Ensure the data at this location is set to the specified value when
  496. * the client is disconnected (due to closing the browser, navigating
  497. * to a new page, or network issues).
  498. *
  499. * onDisconnectSetValue: is especially useful for implementing "presence"
  500. * systems, where a value should be changed or cleared when a user disconnects
  501. * so that he appears "offline" to other users.
  502. *
  503. * @param value The value to be set after the connection is lost.
  504. */
  505. - (void)onDisconnectSetValue:(nullable id)value;
  506. /**
  507. * Ensure the data at this location is set to the specified value when
  508. * the client is disconnected (due to closing the browser, navigating
  509. * to a new page, or network issues).
  510. *
  511. * The completion block will be triggered when the operation has been
  512. * successfully queued up on the Firebase Database servers
  513. *
  514. * @param value The value to be set after the connection is lost.
  515. * @param block Block to be triggered when the operation has been queued up on
  516. * the Firebase Database servers
  517. */
  518. - (void)onDisconnectSetValue:(nullable id)value
  519. withCompletionBlock:(void (^)(NSError *__nullable error,
  520. FIRDatabaseReference *ref))block;
  521. /**
  522. * Ensure the data at this location is set to the specified value and priority
  523. * when the client is disconnected (due to closing the browser, navigating to a
  524. * new page, or network issues).
  525. *
  526. * @param value The value to be set after the connection is lost.
  527. * @param priority The priority to be set after the connection is lost.
  528. */
  529. - (void)onDisconnectSetValue:(nullable id)value andPriority:(id)priority;
  530. /**
  531. * Ensure the data at this location is set to the specified value and priority
  532. * when the client is disconnected (due to closing the browser, navigating to a
  533. * new page, or network issues).
  534. *
  535. * The completion block will be triggered when the operation has been
  536. * successfully queued up on the Firebase Database servers
  537. *
  538. * @param value The value to be set after the connection is lost.
  539. * @param priority The priority to be set after the connection is lost.
  540. * @param block Block to be triggered when the operation has been queued up on
  541. * the Firebase Database servers
  542. */
  543. - (void)onDisconnectSetValue:(nullable id)value
  544. andPriority:(nullable id)priority
  545. withCompletionBlock:(void (^)(NSError *__nullable error,
  546. FIRDatabaseReference *ref))block;
  547. /**
  548. * Ensure the data at this location is removed when
  549. * the client is disconnected (due to closing the app, navigating
  550. * to a new page, or network issues).
  551. *
  552. * onDisconnectRemoveValue is especially useful for implementing "presence"
  553. * systems.
  554. */
  555. - (void)onDisconnectRemoveValue;
  556. /**
  557. * Ensure the data at this location is removed when
  558. * the client is disconnected (due to closing the app, navigating
  559. * to a new page, or network issues).
  560. *
  561. * onDisconnectRemoveValueWithCompletionBlock: is especially useful for
  562. * implementing "presence" systems.
  563. *
  564. * @param block Block to be triggered when the operation has been queued up on
  565. * the Firebase Database servers
  566. */
  567. - (void)onDisconnectRemoveValueWithCompletionBlock:
  568. (void (^)(NSError *__nullable error, FIRDatabaseReference *ref))block;
  569. /**
  570. * Ensure the data has the specified child values updated when
  571. * the client is disconnected (due to closing the browser, navigating
  572. * to a new page, or network issues).
  573. *
  574. *
  575. * @param values A dictionary of child node keys and the values to set them to
  576. * after the connection is lost.
  577. */
  578. - (void)onDisconnectUpdateChildValues:(NSDictionary *)values;
  579. /**
  580. * Ensure the data has the specified child values updated when
  581. * the client is disconnected (due to closing the browser, navigating
  582. * to a new page, or network issues).
  583. *
  584. *
  585. * @param values A dictionary of child node keys and the values to set them to
  586. * after the connection is lost.
  587. * @param block A block that will be called once the operation has been queued
  588. * up on the Firebase Database servers
  589. */
  590. - (void)onDisconnectUpdateChildValues:(NSDictionary *)values
  591. withCompletionBlock:
  592. (void (^)(NSError *__nullable error,
  593. FIRDatabaseReference *ref))block;
  594. /**
  595. * Cancel any operations that are set to run on disconnect. If you previously
  596. * called onDisconnectSetValue:, onDisconnectRemoveValue:, or
  597. * onDisconnectUpdateChildValues:, and no longer want the values updated when
  598. * the connection is lost, call cancelDisconnectOperations:
  599. */
  600. - (void)cancelDisconnectOperations;
  601. /**
  602. * Cancel any operations that are set to run on disconnect. If you previously
  603. * called onDisconnectSetValue:, onDisconnectRemoveValue:, or
  604. * onDisconnectUpdateChildValues:, and no longer want the values updated when
  605. * the connection is lost, call cancelDisconnectOperations:
  606. *
  607. * @param block A block that will be triggered once the Firebase Database
  608. * servers have acknowledged the cancel request.
  609. */
  610. - (void)cancelDisconnectOperationsWithCompletionBlock:
  611. (nullable void (^)(NSError *__nullable error,
  612. FIRDatabaseReference *ref))block;
  613. #pragma mark - Manual Connection Management
  614. /**
  615. * Manually disconnect the Firebase Database client from the server and disable
  616. * automatic reconnection.
  617. *
  618. * The Firebase Database client automatically maintains a persistent connection
  619. * to the Firebase Database server, which will remain active indefinitely and
  620. * reconnect when disconnected. However, the goOffline( ) and goOnline( )
  621. * methods may be used to manually control the client connection in cases where
  622. * a persistent connection is undesirable.
  623. *
  624. * While offline, the Firebase Database client will no longer receive data
  625. * updates from the server. However, all database operations performed locally
  626. * will continue to immediately fire events, allowing your application to
  627. * continue behaving normally. Additionally, each operation performed locally
  628. * will automatically be queued and retried upon reconnection to the Firebase
  629. * Database server.
  630. *
  631. * To reconnect to the Firebase Database server and begin receiving remote
  632. * events, see goOnline( ). Once the connection is reestablished, the Firebase
  633. * Database client will transmit the appropriate data and fire the appropriate
  634. * events so that your client "catches up" automatically.
  635. *
  636. * Note: Invoking this method will impact all Firebase Database connections.
  637. */
  638. + (void)goOffline;
  639. /**
  640. * Manually reestablish a connection to the Firebase Database server and enable
  641. * automatic reconnection.
  642. *
  643. * The Firebase Database client automatically maintains a persistent connection
  644. * to the Firebase Database server, which will remain active indefinitely and
  645. * reconnect when disconnected. However, the goOffline( ) and goOnline( )
  646. * methods may be used to manually control the client connection in cases where
  647. * a persistent connection is undesirable.
  648. *
  649. * This method should be used after invoking goOffline( ) to disable the active
  650. * connection. Once reconnected, the Firebase Database client will automatically
  651. * transmit the proper data and fire the appropriate events so that your client
  652. * "catches up" automatically.
  653. *
  654. * To disconnect from the Firebase Database server, see goOffline( ).
  655. *
  656. * Note: Invoking this method will impact all Firebase Database connections.
  657. */
  658. + (void)goOnline;
  659. #pragma mark - Transactions
  660. /**
  661. * Performs an optimistic-concurrency transactional update to the data at this
  662. * location. Your block will be called with a FIRMutableData instance that
  663. * contains the current data at this location. Your block should update this
  664. * data to the value you wish to write to this location, and then return an
  665. * instance of FIRTransactionResult with the new data.
  666. *
  667. * If, when the operation reaches the server, it turns out that this client had
  668. * stale data, your block will be run again with the latest data from the
  669. * server.
  670. *
  671. * When your block is run, you may decide to abort the transaction by returning
  672. * [FIRTransactionResult abort].
  673. *
  674. * @param block This block receives the current data at this location and must
  675. * return an instance of FIRTransactionResult
  676. */
  677. - (void)runTransactionBlock:
  678. (FIRTransactionResult * (^)(FIRMutableData *currentData))block;
  679. /**
  680. * Performs an optimistic-concurrency transactional update to the data at this
  681. * location. Your block will be called with a FIRMutableData instance that
  682. * contains the current data at this location. Your block should update this
  683. * data to the value you wish to write to this location, and then return an
  684. * instance of FIRTransactionResult with the new data.
  685. *
  686. * If, when the operation reaches the server, it turns out that this client had
  687. * stale data, your block will be run again with the latest data from the
  688. * server.
  689. *
  690. * When your block is run, you may decide to abort the transaction by returning
  691. * [FIRTransactionResult abort].
  692. *
  693. * @param block This block receives the current data at this location and must
  694. * return an instance of FIRTransactionResult
  695. * @param completionBlock This block will be triggered once the transaction is
  696. * complete, whether it was successful or not. It will indicate if there was an
  697. * error, whether or not the data was committed, and what the current value of
  698. * the data at this location is.
  699. */
  700. - (void)runTransactionBlock:
  701. (FIRTransactionResult * (^)(FIRMutableData *currentData))block
  702. andCompletionBlock:
  703. (void (^)(NSError *__nullable error, BOOL committed,
  704. FIRDataSnapshot *__nullable snapshot))completionBlock;
  705. /**
  706. * Performs an optimistic-concurrency transactional update to the data at this
  707. * location. Your block will be called with a FIRMutableData instance that
  708. * contains the current data at this location. Your block should update this
  709. * data to the value you wish to write to this location, and then return an
  710. * instance of FIRTransactionResult with the new data.
  711. *
  712. * If, when the operation reaches the server, it turns out that this client had
  713. * stale data, your block will be run again with the latest data from the
  714. * server.
  715. *
  716. * When your block is run, you may decide to abort the transaction by return
  717. * [FIRTransactionResult abort].
  718. *
  719. * Since your block may be run multiple times, this client could see several
  720. * immediate states that don't exist on the server. You can suppress those
  721. * immediate states until the server confirms the final state of the
  722. * transaction.
  723. *
  724. * @param block This block receives the current data at this location and must
  725. * return an instance of FIRTransactionResult
  726. * @param completionBlock This block will be triggered once the transaction is
  727. * complete, whether it was successful or not. It will indicate if there was an
  728. * error, whether or not the data was committed, and what the current value of
  729. * the data at this location is.
  730. * @param localEvents Set this to NO to suppress events raised for intermediate
  731. * states, and only get events based on the final state of the transaction.
  732. */
  733. - (void)runTransactionBlock:
  734. (FIRTransactionResult * (^)(FIRMutableData *currentData))block
  735. andCompletionBlock:
  736. (nullable void (^)(NSError *__nullable error, BOOL committed,
  737. FIRDataSnapshot *__nullable snapshot))
  738. completionBlock
  739. withLocalEvents:(BOOL)localEvents;
  740. #pragma mark - Retrieving String Representation
  741. /**
  742. * Gets the absolute URL of this Firebase Database location.
  743. *
  744. * @return The absolute URL of the referenced Firebase Database location.
  745. */
  746. - (NSString *)description;
  747. #pragma mark - Properties
  748. /**
  749. * Gets a FIRDatabaseReference for the parent location.
  750. * If this instance refers to the root of your Firebase Database, it has no
  751. * parent, and therefore parent( ) will return null.
  752. *
  753. * @return A FIRDatabaseReference for the parent location.
  754. */
  755. @property(strong, readonly, nonatomic, nullable) FIRDatabaseReference *parent;
  756. /**
  757. * Gets a FIRDatabaseReference for the root location
  758. *
  759. * @return A new FIRDatabaseReference to root location.
  760. */
  761. @property(strong, readonly, nonatomic) FIRDatabaseReference *root;
  762. /**
  763. * Gets the last token in a Firebase Database location (e.g. 'fred' in
  764. * https&#58;//SampleChat.firebaseIO-demo.com/users/fred)
  765. *
  766. * @return The key of the location this reference points to.
  767. */
  768. @property(strong, readonly, nonatomic, nullable) NSString *key;
  769. /**
  770. * Gets the URL for the Firebase Database location referenced by this
  771. * FIRDatabaseReference.
  772. *
  773. * @return The url of the location this reference points to.
  774. */
  775. @property(strong, readonly, nonatomic) NSString *URL;
  776. /**
  777. * Gets the FIRDatabase instance associated with this reference.
  778. *
  779. * @return The FIRDatabase object for this reference.
  780. */
  781. @property(strong, readonly, nonatomic) FIRDatabase *database;
  782. @end
  783. NS_ASSUME_NONNULL_END