FIRDatabaseReference.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabaseReference.h"
  17. #import "FirebaseCore/Internal/FirebaseCoreInternal.h"
  18. #import "FirebaseDatabase/Sources/Api/FIRDatabaseConfig.h"
  19. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  20. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseReference_Private.h"
  21. #import "FirebaseDatabase/Sources/Core/FQueryParams.h"
  22. #import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
  23. #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabase.h"
  24. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  25. #import "FirebaseDatabase/Sources/Utilities/FNextPushId.h"
  26. #import "FirebaseDatabase/Sources/Utilities/FStringUtilities.h"
  27. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  28. #import "FirebaseDatabase/Sources/Utilities/FValidation.h"
  29. @implementation FIRDatabaseReference
  30. #pragma mark -
  31. #pragma mark Constructors
  32. - (id)initWithConfig:(FIRDatabaseConfig *)config {
  33. FParsedUrl *parsedUrl =
  34. [FUtilities parseUrl:[[FIRApp defaultApp] options].databaseURL];
  35. [FValidation validateFrom:@"initWithUrl:" validURL:parsedUrl];
  36. return [self initWithRepo:[FRepoManager getRepo:parsedUrl.repoInfo
  37. config:config]
  38. path:parsedUrl.path];
  39. }
  40. - (id)initWithRepo:(FRepo *)repo path:(FPath *)path {
  41. return [super initWithRepo:repo
  42. path:path
  43. params:[FQueryParams defaultInstance]
  44. orderByCalled:NO
  45. priorityMethodCalled:NO];
  46. }
  47. #pragma mark -
  48. #pragma mark Ancillary methods
  49. - (nullable NSString *)key {
  50. if ([self.path isEmpty]) {
  51. return nil;
  52. } else {
  53. return [self.path getBack];
  54. }
  55. }
  56. - (FIRDatabase *)database {
  57. return self.repo.database;
  58. }
  59. - (FIRDatabaseReference *)parent {
  60. FPath *parentPath = [self.path parent];
  61. FIRDatabaseReference *parent = nil;
  62. if (parentPath != nil) {
  63. parent = [[FIRDatabaseReference alloc] initWithRepo:self.repo
  64. path:parentPath];
  65. }
  66. return parent;
  67. }
  68. - (NSString *)URL {
  69. FIRDatabaseReference *parent = [self parent];
  70. return parent == nil
  71. ? [self.repo description]
  72. : [NSString
  73. stringWithFormat:@"%@/%@", [parent description],
  74. [FStringUtilities urlEncoded:self.key]];
  75. }
  76. - (NSString *)description {
  77. return [self URL];
  78. }
  79. - (FIRDatabaseReference *)root {
  80. return [[FIRDatabaseReference alloc]
  81. initWithRepo:self.repo
  82. path:[[FPath alloc] initWith:@""]];
  83. }
  84. #pragma mark -
  85. #pragma mark Child methods
  86. - (FIRDatabaseReference *)child:(NSString *)pathString {
  87. if ([self.path getFront] == nil) {
  88. // we're at the root
  89. [FValidation validateFrom:@"child:" validRootPathString:pathString];
  90. } else {
  91. [FValidation validateFrom:@"child:" validPathString:pathString];
  92. }
  93. FPath *path = [self.path childFromString:pathString];
  94. FIRDatabaseReference *firebaseRef =
  95. [[FIRDatabaseReference alloc] initWithRepo:self.repo path:path];
  96. return firebaseRef;
  97. }
  98. - (FIRDatabaseReference *)childByAutoId {
  99. [FValidation validateFrom:@"childByAutoId:" writablePath:self.path];
  100. NSString *name = [FNextPushId get:self.repo.serverTime];
  101. return [self child:name];
  102. }
  103. #pragma mark -
  104. #pragma mark Basic write methods
  105. - (void)setValue:(id)value {
  106. [self setValueInternal:value
  107. andPriority:nil
  108. withCompletionBlock:nil
  109. from:@"setValue:"];
  110. }
  111. - (void)setValue:(id)value withCompletionBlock:(fbt_void_nserror_ref)block {
  112. [self setValueInternal:value
  113. andPriority:nil
  114. withCompletionBlock:block
  115. from:@"setValue:withCompletionBlock:"];
  116. }
  117. - (void)setValue:(id)value andPriority:(id)priority {
  118. [self setValueInternal:value
  119. andPriority:priority
  120. withCompletionBlock:nil
  121. from:@"setValue:andPriority:"];
  122. }
  123. - (void)setValue:(id)value
  124. andPriority:(id)priority
  125. withCompletionBlock:(fbt_void_nserror_ref)block {
  126. [self setValueInternal:value
  127. andPriority:priority
  128. withCompletionBlock:block
  129. from:@"setValue:andPriority:withCompletionBlock:"];
  130. }
  131. - (void)setValueInternal:(id)value
  132. andPriority:(id)priority
  133. withCompletionBlock:(fbt_void_nserror_ref)block
  134. from:(NSString *)fn {
  135. [FValidation validateFrom:fn writablePath:self.path];
  136. fbt_void_nserror_ref userCallback = [block copy];
  137. id<FNode> newNode = [FSnapshotUtilities nodeFrom:value
  138. priority:priority
  139. withValidationFrom:fn];
  140. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  141. [self.repo set:self.path withNode:newNode withCallback:userCallback];
  142. });
  143. }
  144. - (void)removeValue {
  145. [self setValueInternal:nil
  146. andPriority:nil
  147. withCompletionBlock:nil
  148. from:@"removeValue:"];
  149. }
  150. - (void)removeValueWithCompletionBlock:(fbt_void_nserror_ref)block {
  151. [self setValueInternal:nil
  152. andPriority:nil
  153. withCompletionBlock:block
  154. from:@"removeValueWithCompletionBlock:"];
  155. }
  156. - (void)setPriority:(id)priority {
  157. [self setPriorityInternal:priority
  158. withCompletionBlock:nil
  159. from:@"setPriority:"];
  160. }
  161. - (void)setPriority:(id)priority
  162. withCompletionBlock:(fbt_void_nserror_ref)block {
  163. [self setPriorityInternal:priority
  164. withCompletionBlock:block
  165. from:@"setPriority:withCompletionBlock:"];
  166. }
  167. - (void)setPriorityInternal:(id)priority
  168. withCompletionBlock:(fbt_void_nserror_ref)block
  169. from:(NSString *)fn {
  170. [FValidation validateFrom:fn writablePath:self.path];
  171. fbt_void_nserror_ref userCallback = [block copy];
  172. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  173. [self.repo set:[self.path childFromString:@".priority"]
  174. withNode:[FSnapshotUtilities nodeFrom:priority]
  175. withCallback:userCallback];
  176. });
  177. }
  178. - (void)updateChildValues:(NSDictionary *)values {
  179. [self updateChildValuesInternal:values
  180. withCompletionBlock:nil
  181. from:@"updateChildValues:"];
  182. }
  183. - (void)updateChildValues:(NSDictionary *)values
  184. withCompletionBlock:(fbt_void_nserror_ref)block {
  185. [self updateChildValuesInternal:values
  186. withCompletionBlock:block
  187. from:@"updateChildValues:withCompletionBlock:"];
  188. }
  189. - (void)updateChildValuesInternal:(NSDictionary *)values
  190. withCompletionBlock:(fbt_void_nserror_ref)block
  191. from:(NSString *)fn {
  192. [FValidation validateFrom:fn writablePath:self.path];
  193. FCompoundWrite *merge =
  194. [FSnapshotUtilities compoundWriteFromDictionary:values
  195. withValidationFrom:fn];
  196. fbt_void_nserror_ref userCallback = [block copy];
  197. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  198. [self.repo update:self.path withNodes:merge withCallback:userCallback];
  199. });
  200. }
  201. #pragma mark -
  202. #pragma mark Disconnect Operations
  203. - (void)onDisconnectSetValue:(id)value {
  204. [self onDisconnectSetValueInternal:value
  205. andPriority:nil
  206. withCompletionBlock:nil
  207. from:@"onDisconnectSetValue:"];
  208. }
  209. - (void)onDisconnectSetValue:(id)value
  210. withCompletionBlock:(fbt_void_nserror_ref)block {
  211. [self onDisconnectSetValueInternal:value
  212. andPriority:nil
  213. withCompletionBlock:block
  214. from:@"onDisconnectSetValue:"
  215. @"withCompletionBlock:"];
  216. }
  217. - (void)onDisconnectSetValue:(id)value andPriority:(id)priority {
  218. [self onDisconnectSetValueInternal:value
  219. andPriority:priority
  220. withCompletionBlock:nil
  221. from:@"onDisconnectSetValue:andPriority:"];
  222. }
  223. - (void)onDisconnectSetValue:(id)value
  224. andPriority:(id)priority
  225. withCompletionBlock:(fbt_void_nserror_ref)block {
  226. [self onDisconnectSetValueInternal:value
  227. andPriority:priority
  228. withCompletionBlock:block
  229. from:@"onDisconnectSetValue:andPriority:"
  230. @"withCompletionBlock:"];
  231. }
  232. - (void)onDisconnectSetValueInternal:(id)value
  233. andPriority:(id)priority
  234. withCompletionBlock:(fbt_void_nserror_ref)block
  235. from:(NSString *)fn {
  236. [FValidation validateFrom:fn writablePath:self.path];
  237. id<FNode> newNodeUnresolved = [FSnapshotUtilities nodeFrom:value
  238. priority:priority
  239. withValidationFrom:fn];
  240. fbt_void_nserror_ref userCallback = [block copy];
  241. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  242. [self.repo onDisconnectSet:self.path
  243. withNode:newNodeUnresolved
  244. withCallback:userCallback];
  245. });
  246. }
  247. - (void)onDisconnectRemoveValue {
  248. [self onDisconnectSetValueInternal:nil
  249. andPriority:nil
  250. withCompletionBlock:nil
  251. from:@"onDisconnectRemoveValue:"];
  252. }
  253. - (void)onDisconnectRemoveValueWithCompletionBlock:(fbt_void_nserror_ref)block {
  254. [self onDisconnectSetValueInternal:nil
  255. andPriority:nil
  256. withCompletionBlock:block
  257. from:@"onDisconnectRemoveValueWithCompletionB"
  258. @"lock:"];
  259. }
  260. - (void)onDisconnectUpdateChildValues:(NSDictionary *)values {
  261. [self
  262. onDisconnectUpdateChildValuesInternal:values
  263. withCompletionBlock:nil
  264. from:
  265. @"onDisconnectUpdateChildValues:"];
  266. }
  267. - (void)onDisconnectUpdateChildValues:(NSDictionary *)values
  268. withCompletionBlock:(fbt_void_nserror_ref)block {
  269. [self onDisconnectUpdateChildValuesInternal:values
  270. withCompletionBlock:block
  271. from:@"onDisconnectUpdateChildValues"
  272. @":withCompletionBlock:"];
  273. }
  274. - (void)onDisconnectUpdateChildValuesInternal:(NSDictionary *)values
  275. withCompletionBlock:(fbt_void_nserror_ref)block
  276. from:(NSString *)fn {
  277. [FValidation validateFrom:fn writablePath:self.path];
  278. FCompoundWrite *merge =
  279. [FSnapshotUtilities compoundWriteFromDictionary:values
  280. withValidationFrom:fn];
  281. fbt_void_nserror_ref userCallback = [block copy];
  282. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  283. [self.repo onDisconnectUpdate:self.path
  284. withNodes:merge
  285. withCallback:userCallback];
  286. });
  287. }
  288. - (void)cancelDisconnectOperations {
  289. [self cancelDisconnectOperationsWithCompletionBlock:nil];
  290. }
  291. - (void)cancelDisconnectOperationsWithCompletionBlock:
  292. (fbt_void_nserror_ref)block {
  293. fbt_void_nserror_ref callback = nil;
  294. if (block != nil) {
  295. callback = [block copy];
  296. }
  297. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  298. [self.repo onDisconnectCancel:self.path withCallback:callback];
  299. });
  300. }
  301. #pragma mark -
  302. #pragma mark Connection management methods
  303. + (void)goOffline {
  304. [FRepoManager interruptAll];
  305. }
  306. + (void)goOnline {
  307. [FRepoManager resumeAll];
  308. }
  309. #pragma mark -
  310. #pragma mark Data reading methods deferred to FQuery
  311. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  312. withBlock:(fbt_void_datasnapshot)block {
  313. return [self observeEventType:eventType
  314. withBlock:block
  315. withCancelBlock:nil];
  316. }
  317. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  318. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
  319. return [self observeEventType:eventType
  320. andPreviousSiblingKeyWithBlock:block
  321. withCancelBlock:nil];
  322. }
  323. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  324. withBlock:(fbt_void_datasnapshot)block
  325. withCancelBlock:(fbt_void_nserror)cancelBlock {
  326. return [super observeEventType:eventType
  327. withBlock:block
  328. withCancelBlock:cancelBlock];
  329. }
  330. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  331. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block
  332. withCancelBlock:(fbt_void_nserror)cancelBlock {
  333. return [super observeEventType:eventType
  334. andPreviousSiblingKeyWithBlock:block
  335. withCancelBlock:cancelBlock];
  336. }
  337. - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle {
  338. [super removeObserverWithHandle:handle];
  339. }
  340. - (void)removeAllObservers {
  341. [super removeAllObservers];
  342. }
  343. - (void)keepSynced:(BOOL)keepSynced {
  344. [super keepSynced:keepSynced];
  345. }
  346. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  347. withBlock:(fbt_void_datasnapshot)block {
  348. [self observeSingleEventOfType:eventType
  349. withBlock:block
  350. withCancelBlock:nil];
  351. }
  352. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  353. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
  354. [self observeSingleEventOfType:eventType
  355. andPreviousSiblingKeyWithBlock:block
  356. withCancelBlock:nil];
  357. }
  358. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  359. withBlock:(fbt_void_datasnapshot)block
  360. withCancelBlock:(fbt_void_nserror)cancelBlock {
  361. [super observeSingleEventOfType:eventType
  362. withBlock:block
  363. withCancelBlock:cancelBlock];
  364. }
  365. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  366. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block
  367. withCancelBlock:(fbt_void_nserror)cancelBlock {
  368. [super observeSingleEventOfType:eventType
  369. andPreviousSiblingKeyWithBlock:block
  370. withCancelBlock:cancelBlock];
  371. }
  372. #pragma mark -
  373. #pragma mark Query methods
  374. // These methods suppress warnings from having method definitions in
  375. // FIRDatabaseReference.h for docs generation.
  376. - (void)getDataWithCompletionBlock:
  377. (void (^_Nonnull)(NSError *__nullable error,
  378. FIRDataSnapshot *snapshot))block {
  379. [super getDataWithCompletionBlock:block];
  380. }
  381. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit {
  382. return [super queryLimitedToFirst:limit];
  383. }
  384. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit {
  385. return [super queryLimitedToLast:limit];
  386. }
  387. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key {
  388. return [super queryOrderedByChild:key];
  389. }
  390. - (FIRDatabaseQuery *)queryOrderedByKey {
  391. return [super queryOrderedByKey];
  392. }
  393. - (FIRDatabaseQuery *)queryOrderedByPriority {
  394. return [super queryOrderedByPriority];
  395. }
  396. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue {
  397. return [super queryStartingAtValue:startValue];
  398. }
  399. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue
  400. childKey:(NSString *)childKey {
  401. return [super queryStartingAtValue:startValue childKey:childKey];
  402. }
  403. - (FIRDatabaseQuery *)queryStartingAfterValue:(id)startAfterValue {
  404. return [super queryStartingAfterValue:startAfterValue];
  405. }
  406. - (FIRDatabaseQuery *)queryStartingAfterValue:(id)startAfterValue
  407. childKey:(NSString *)childKey {
  408. return [super queryStartingAfterValue:startAfterValue childKey:childKey];
  409. }
  410. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue {
  411. return [super queryEndingAtValue:endValue];
  412. }
  413. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue
  414. childKey:(NSString *)childKey {
  415. return [super queryEndingAtValue:endValue childKey:childKey];
  416. }
  417. - (FIRDatabaseQuery *)queryEqualToValue:(id)value {
  418. return [super queryEqualToValue:value];
  419. }
  420. - (FIRDatabaseQuery *)queryEqualToValue:(id)value
  421. childKey:(NSString *)childKey {
  422. return [super queryEqualToValue:value childKey:childKey];
  423. }
  424. #pragma mark -
  425. #pragma mark Transaction methods
  426. - (void)runTransactionBlock:(fbt_transactionresult_mutabledata)block {
  427. [FValidation validateFrom:@"runTransactionBlock:" writablePath:self.path];
  428. [self runTransactionBlock:block andCompletionBlock:nil withLocalEvents:YES];
  429. }
  430. - (void)runTransactionBlock:(fbt_transactionresult_mutabledata)update
  431. andCompletionBlock:
  432. (fbt_void_nserror_bool_datasnapshot)completionBlock {
  433. [FValidation validateFrom:@"runTransactionBlock:andCompletionBlock:"
  434. writablePath:self.path];
  435. [self runTransactionBlock:update
  436. andCompletionBlock:completionBlock
  437. withLocalEvents:YES];
  438. }
  439. - (void)runTransactionBlock:(fbt_transactionresult_mutabledata)block
  440. andCompletionBlock:(fbt_void_nserror_bool_datasnapshot)completionBlock
  441. withLocalEvents:(BOOL)localEvents {
  442. [FValidation
  443. validateFrom:@"runTransactionBlock:andCompletionBlock:withLocalEvents:"
  444. writablePath:self.path];
  445. fbt_transactionresult_mutabledata updateCopy = [block copy];
  446. fbt_void_nserror_bool_datasnapshot onCompleteCopy = [completionBlock copy];
  447. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  448. [self.repo startTransactionOnPath:self.path
  449. update:updateCopy
  450. onComplete:onCompleteCopy
  451. withLocalEvents:localEvents];
  452. });
  453. }
  454. @end