FIRDatabaseReference.h 34 KB

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