FIRDatabaseReference.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/Sources/Private/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. FFLog(@"I-RDB038028", @"Running local firebase ios sdk implementation!");
  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. - (void)getDataWithCompletionBlock:
  378. (void (^_Nonnull)(NSError *__nullable error,
  379. FIRDataSnapshot *snapshot))block {
  380. [super getDataWithCompletionBlock:block];
  381. }
  382. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit {
  383. return [super queryLimitedToFirst:limit];
  384. }
  385. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit {
  386. return [super queryLimitedToLast:limit];
  387. }
  388. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key {
  389. return [super queryOrderedByChild:key];
  390. }
  391. - (FIRDatabaseQuery *)queryOrderedByKey {
  392. return [super queryOrderedByKey];
  393. }
  394. - (FIRDatabaseQuery *)queryOrderedByPriority {
  395. return [super queryOrderedByPriority];
  396. }
  397. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue {
  398. return [super queryStartingAtValue:startValue];
  399. }
  400. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue
  401. childKey:(NSString *)childKey {
  402. return [super queryStartingAtValue:startValue childKey:childKey];
  403. }
  404. - (FIRDatabaseQuery *)queryStartingAfterValue:(id)startAfterValue {
  405. return [super queryStartingAfterValue:startAfterValue];
  406. }
  407. - (FIRDatabaseQuery *)queryStartingAfterValue:(id)startAfterValue
  408. childKey:(NSString *)childKey {
  409. return [super queryStartingAfterValue:startAfterValue childKey:childKey];
  410. }
  411. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue {
  412. return [super queryEndingAtValue:endValue];
  413. }
  414. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue
  415. childKey:(NSString *)childKey {
  416. return [super queryEndingAtValue:endValue childKey:childKey];
  417. }
  418. - (FIRDatabaseQuery *)queryEqualToValue:(id)value {
  419. return [super queryEqualToValue:value];
  420. }
  421. - (FIRDatabaseQuery *)queryEqualToValue:(id)value
  422. childKey:(NSString *)childKey {
  423. return [super queryEqualToValue:value childKey:childKey];
  424. }
  425. #pragma mark -
  426. #pragma mark Transaction methods
  427. - (void)runTransactionBlock:(fbt_transactionresult_mutabledata)block {
  428. [FValidation validateFrom:@"runTransactionBlock:" writablePath:self.path];
  429. [self runTransactionBlock:block andCompletionBlock:nil withLocalEvents:YES];
  430. }
  431. - (void)runTransactionBlock:(fbt_transactionresult_mutabledata)update
  432. andCompletionBlock:
  433. (fbt_void_nserror_bool_datasnapshot)completionBlock {
  434. [FValidation validateFrom:@"runTransactionBlock:andCompletionBlock:"
  435. writablePath:self.path];
  436. [self runTransactionBlock:update
  437. andCompletionBlock:completionBlock
  438. withLocalEvents:YES];
  439. }
  440. - (void)runTransactionBlock:(fbt_transactionresult_mutabledata)block
  441. andCompletionBlock:(fbt_void_nserror_bool_datasnapshot)completionBlock
  442. withLocalEvents:(BOOL)localEvents {
  443. [FValidation
  444. validateFrom:@"runTransactionBlock:andCompletionBlock:withLocalEvents:"
  445. writablePath:self.path];
  446. fbt_transactionresult_mutabledata updateCopy = [block copy];
  447. fbt_void_nserror_bool_datasnapshot onCompleteCopy = [completionBlock copy];
  448. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  449. [self.repo startTransactionOnPath:self.path
  450. update:updateCopy
  451. onComplete:onCompleteCopy
  452. withLocalEvents:localEvents];
  453. });
  454. }
  455. @end