FIRDatabaseReference.m 17 KB

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