FIRDatabaseReference.h 33 KB

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