FIRDatabaseReference.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <FirebaseCore/FIRApp.h>
  17. #import "FIRDatabaseReference.h"
  18. #import <FirebaseCore/FIROptions.h>
  19. #import "FUtilities.h"
  20. #import "FNextPushId.h"
  21. #import "FIRDatabaseQuery_Private.h"
  22. #import "FValidation.h"
  23. #import "FIRDatabaseReference_Private.h"
  24. #import "FStringUtilities.h"
  25. #import "FSnapshotUtilities.h"
  26. #import "FIRDatabaseConfig.h"
  27. #import "FIRDatabaseConfig_Private.h"
  28. #import "FQueryParams.h"
  29. #import "FIRDatabase.h"
  30. @implementation FIRDatabaseReference
  31. #pragma mark -
  32. #pragma mark Constructors
  33. - (id) initWithConfig:(FIRDatabaseConfig *)config {
  34. FParsedUrl* parsedUrl = [FUtilities parseUrl:[[FIRApp defaultApp] options].databaseURL];
  35. [FValidation validateFrom:@"initWithUrl:" validURL:parsedUrl];
  36. return [self initWithRepo:[FRepoManager getRepo:parsedUrl.repoInfo config:config] path:parsedUrl.path];
  37. }
  38. - (id) initWithRepo:(FRepo *)repo path:(FPath *)path {
  39. return [super initWithRepo:repo
  40. path:path
  41. params:[FQueryParams defaultInstance]
  42. orderByCalled:NO
  43. priorityMethodCalled:NO];
  44. }
  45. #pragma mark -
  46. #pragma mark Ancillary methods
  47. - (nullable NSString *) key {
  48. if([self.path isEmpty]) {
  49. return nil;
  50. }
  51. else {
  52. return [self.path getBack];
  53. }
  54. }
  55. - (FIRDatabase *) database {
  56. return self.repo.database;
  57. }
  58. - (FIRDatabaseReference *) parent {
  59. FPath* parentPath = [self.path parent];
  60. FIRDatabaseReference * parent = nil;
  61. if (parentPath != nil ) {
  62. parent = [[FIRDatabaseReference alloc] initWithRepo:self.repo path:parentPath];
  63. }
  64. return parent;
  65. }
  66. - (NSString *) URL {
  67. FIRDatabaseReference * parent = [self parent];
  68. return parent == nil ? [self.repo description] : [NSString stringWithFormat:@"%@/%@", [parent description], [FStringUtilities urlEncoded:self.key]];
  69. }
  70. - (NSString *) description {
  71. return [self URL];
  72. }
  73. - (FIRDatabaseReference *) root {
  74. return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:[[FPath alloc] initWith:@""]];
  75. }
  76. #pragma mark -
  77. #pragma mark Child methods
  78. - (FIRDatabaseReference *)childByAppendingPath:(NSString *)pathString {
  79. return [self child:pathString];
  80. }
  81. - (FIRDatabaseReference *)child:(NSString *)pathString {
  82. if ([self.path getFront] == nil) {
  83. // we're at the root
  84. [FValidation validateFrom:@"child:" validRootPathString:pathString];
  85. } else {
  86. [FValidation validateFrom:@"child:" validPathString:pathString];
  87. }
  88. FPath* path = [self.path childFromString:pathString];
  89. FIRDatabaseReference * firebaseRef = [[FIRDatabaseReference alloc] initWithRepo:self.repo path:path];
  90. return firebaseRef;
  91. }
  92. - (FIRDatabaseReference *) childByAutoId {
  93. [FValidation validateFrom:@"childByAutoId:" writablePath:self.path];
  94. NSString* name = [FNextPushId get:self.repo.serverTime];
  95. return [self child:name];
  96. }
  97. #pragma mark -
  98. #pragma mark Basic write methods
  99. - (void) setValue:(id)value {
  100. [self setValueInternal:value andPriority:nil withCompletionBlock:nil from:@"setValue:"];
  101. }
  102. - (void) setValue:(id)value withCompletionBlock:(fbt_void_nserror_ref)block {
  103. [self setValueInternal:value andPriority:nil withCompletionBlock:block from:@"setValue:withCompletionBlock:"];
  104. }
  105. - (void) setValue:(id)value andPriority:(id)priority {
  106. [self setValueInternal:value andPriority:priority withCompletionBlock:nil from:@"setValue:andPriority:"];
  107. }
  108. - (void) setValue:(id)value andPriority:(id)priority withCompletionBlock:(fbt_void_nserror_ref)block {
  109. [self setValueInternal:value andPriority:priority withCompletionBlock:block from:@"setValue:andPriority:withCompletionBlock:"];
  110. }
  111. - (void) setValueInternal:(id)value andPriority:(id)priority withCompletionBlock:(fbt_void_nserror_ref)block from:(NSString*)fn {
  112. [FValidation validateFrom:fn writablePath:self.path];
  113. fbt_void_nserror_ref userCallback = [block copy];
  114. id<FNode> newNode = [FSnapshotUtilities nodeFrom:value priority:priority withValidationFrom:fn];
  115. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  116. [self.repo set:self.path withNode:newNode withCallback:userCallback];
  117. });
  118. }
  119. - (void) removeValue {
  120. [self setValueInternal:nil andPriority:nil withCompletionBlock:nil from:@"removeValue:"];
  121. }
  122. - (void) removeValueWithCompletionBlock:(fbt_void_nserror_ref)block {
  123. [self setValueInternal:nil andPriority:nil withCompletionBlock:block from:@"removeValueWithCompletionBlock:"];
  124. }
  125. - (void) setPriority:(id)priority {
  126. [self setPriorityInternal:priority withCompletionBlock:nil from:@"setPriority:"];
  127. }
  128. - (void) setPriority:(id)priority withCompletionBlock:(fbt_void_nserror_ref)block {
  129. [self setPriorityInternal:priority withCompletionBlock:block from:@"setPriority:withCompletionBlock:"];
  130. }
  131. - (void) setPriorityInternal:(id)priority withCompletionBlock:(fbt_void_nserror_ref)block from:(NSString*)fn {
  132. [FValidation validateFrom:fn writablePath:self.path];
  133. fbt_void_nserror_ref userCallback = [block copy];
  134. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  135. [self.repo set:[self.path childFromString:@".priority"] withNode:[FSnapshotUtilities nodeFrom:priority] withCallback:userCallback];
  136. });
  137. }
  138. - (void) updateChildValues:(NSDictionary *)values {
  139. [self updateChildValuesInternal:values withCompletionBlock:nil from:@"updateChildValues:"];
  140. }
  141. - (void) updateChildValues:(NSDictionary *)values withCompletionBlock:(fbt_void_nserror_ref)block {
  142. [self updateChildValuesInternal:values withCompletionBlock:block from:@"updateChildValues:withCompletionBlock:"];
  143. }
  144. - (void) updateChildValuesInternal:(NSDictionary *)values withCompletionBlock:(fbt_void_nserror_ref)block from:(NSString*)fn {
  145. [FValidation validateFrom:fn writablePath:self.path];
  146. FCompoundWrite *merge = [FSnapshotUtilities compoundWriteFromDictionary:values withValidationFrom:fn];
  147. fbt_void_nserror_ref userCallback = [block copy];
  148. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  149. [self.repo update:self.path withNodes:merge withCallback:userCallback];
  150. });
  151. }
  152. #pragma mark -
  153. #pragma mark Disconnect Operations
  154. - (void) onDisconnectSetValue:(id)value {
  155. [self onDisconnectSetValueInternal:value andPriority:nil withCompletionBlock:nil from:@"onDisconnectSetValue:"];
  156. }
  157. - (void) onDisconnectSetValue:(id)value withCompletionBlock:(fbt_void_nserror_ref)block {
  158. [self onDisconnectSetValueInternal:value andPriority:nil withCompletionBlock:block from:@"onDisconnectSetValue:withCompletionBlock:"];
  159. }
  160. - (void) onDisconnectSetValue:(id)value andPriority:(id)priority {
  161. [self onDisconnectSetValueInternal:value andPriority:priority withCompletionBlock:nil from:@"onDisconnectSetValue:andPriority:"];
  162. }
  163. - (void) onDisconnectSetValue:(id)value andPriority:(id)priority withCompletionBlock:(fbt_void_nserror_ref)block {
  164. [self onDisconnectSetValueInternal:value andPriority:priority withCompletionBlock:block from:@"onDisconnectSetValue:andPriority:withCompletionBlock:"];
  165. }
  166. - (void) onDisconnectSetValueInternal:(id)value andPriority:(id)priority withCompletionBlock:(fbt_void_nserror_ref)block from:(NSString*)fn {
  167. [FValidation validateFrom:fn writablePath:self.path];
  168. id<FNode> newNodeUnresolved = [FSnapshotUtilities nodeFrom:value priority:priority withValidationFrom:fn];
  169. fbt_void_nserror_ref userCallback = [block copy];
  170. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  171. [self.repo onDisconnectSet:self.path withNode:newNodeUnresolved withCallback:userCallback];
  172. });
  173. }
  174. - (void) onDisconnectRemoveValue {
  175. [self onDisconnectSetValueInternal:nil andPriority:nil withCompletionBlock:nil from:@"onDisconnectRemoveValue:"];
  176. }
  177. - (void) onDisconnectRemoveValueWithCompletionBlock:(fbt_void_nserror_ref)block {
  178. [self onDisconnectSetValueInternal:nil andPriority:nil withCompletionBlock:block from:@"onDisconnectRemoveValueWithCompletionBlock:"];
  179. }
  180. - (void) onDisconnectUpdateChildValues:(NSDictionary *)values {
  181. [self onDisconnectUpdateChildValuesInternal:values withCompletionBlock:nil from:@"onDisconnectUpdateChildValues:"];
  182. }
  183. - (void) onDisconnectUpdateChildValues:(NSDictionary *)values withCompletionBlock:(fbt_void_nserror_ref)block {
  184. [self onDisconnectUpdateChildValuesInternal:values withCompletionBlock:block from:@"onDisconnectUpdateChildValues:withCompletionBlock:"];
  185. }
  186. - (void) onDisconnectUpdateChildValuesInternal:(NSDictionary *)values withCompletionBlock:(fbt_void_nserror_ref)block from:(NSString*)fn {
  187. [FValidation validateFrom:fn writablePath:self.path];
  188. FCompoundWrite *merge = [FSnapshotUtilities compoundWriteFromDictionary:values withValidationFrom:fn];
  189. fbt_void_nserror_ref userCallback = [block copy];
  190. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  191. [self.repo onDisconnectUpdate:self.path withNodes:merge withCallback:userCallback];
  192. });
  193. }
  194. - (void) cancelDisconnectOperations {
  195. [self cancelDisconnectOperationsWithCompletionBlock:nil];
  196. }
  197. - (void) cancelDisconnectOperationsWithCompletionBlock:(fbt_void_nserror_ref)block {
  198. fbt_void_nserror_ref callback = nil;
  199. if (block != nil) {
  200. callback = [block copy];
  201. }
  202. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  203. [self.repo onDisconnectCancel:self.path withCallback:callback];
  204. });
  205. }
  206. #pragma mark -
  207. #pragma mark Connection management methods
  208. + (void) goOffline {
  209. [FRepoManager interruptAll];
  210. }
  211. + (void) goOnline {
  212. [FRepoManager resumeAll];
  213. }
  214. #pragma mark -
  215. #pragma mark Data reading methods deferred to FQuery
  216. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType withBlock:(fbt_void_datasnapshot)block {
  217. return [self observeEventType:eventType withBlock:block withCancelBlock:nil];
  218. }
  219. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
  220. return [self observeEventType:eventType andPreviousSiblingKeyWithBlock:block withCancelBlock:nil];
  221. }
  222. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType withBlock:(fbt_void_datasnapshot)block withCancelBlock:(fbt_void_nserror)cancelBlock {
  223. return [super observeEventType:eventType withBlock:block withCancelBlock:cancelBlock];
  224. }
  225. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block withCancelBlock:(fbt_void_nserror)cancelBlock {
  226. return [super observeEventType:eventType andPreviousSiblingKeyWithBlock:block withCancelBlock:cancelBlock];
  227. }
  228. - (void) removeObserverWithHandle:(FIRDatabaseHandle)handle {
  229. [super removeObserverWithHandle:handle];
  230. }
  231. - (void) removeAllObservers {
  232. [super removeAllObservers];
  233. }
  234. - (void) keepSynced:(BOOL)keepSynced {
  235. [super keepSynced:keepSynced];
  236. }
  237. - (void)observeSingleEventOfType:(FIRDataEventType)eventType withBlock:(fbt_void_datasnapshot)block {
  238. [self observeSingleEventOfType:eventType withBlock:block withCancelBlock:nil];
  239. }
  240. - (void)observeSingleEventOfType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
  241. [self observeSingleEventOfType:eventType andPreviousSiblingKeyWithBlock:block withCancelBlock:nil];
  242. }
  243. - (void)observeSingleEventOfType:(FIRDataEventType)eventType withBlock:(fbt_void_datasnapshot)block withCancelBlock:(fbt_void_nserror)cancelBlock {
  244. [super observeSingleEventOfType:eventType withBlock:block withCancelBlock:cancelBlock];
  245. }
  246. - (void)observeSingleEventOfType:(FIRDataEventType)eventType andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block withCancelBlock:(fbt_void_nserror)cancelBlock {
  247. [super observeSingleEventOfType:eventType andPreviousSiblingKeyWithBlock:block withCancelBlock:cancelBlock];
  248. }
  249. #pragma mark -
  250. #pragma mark Query methods
  251. // These methods suppress warnings from having method definitions in FIRDatabaseReference.h for docs generation.
  252. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit {
  253. return [super queryLimitedToFirst:limit];
  254. }
  255. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit {
  256. return [super queryLimitedToLast:limit];
  257. }
  258. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)key {
  259. return [super queryOrderedByChild:key];
  260. }
  261. - (FIRDatabaseQuery *) queryOrderedByKey {
  262. return [super queryOrderedByKey];
  263. }
  264. - (FIRDatabaseQuery *) queryOrderedByPriority {
  265. return [super queryOrderedByPriority];
  266. }
  267. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue {
  268. return [super queryStartingAtValue:startValue];
  269. }
  270. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue childKey:(NSString *)childKey {
  271. return [super queryStartingAtValue:startValue childKey:childKey];
  272. }
  273. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue {
  274. return [super queryEndingAtValue:endValue];
  275. }
  276. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue childKey:(NSString *)childKey {
  277. return [super queryEndingAtValue:endValue childKey:childKey];
  278. }
  279. - (FIRDatabaseQuery *)queryEqualToValue:(id)value {
  280. return [super queryEqualToValue:value];
  281. }
  282. - (FIRDatabaseQuery *)queryEqualToValue:(id)value childKey:(NSString *)childKey {
  283. return [super queryEqualToValue:value childKey:childKey];
  284. }
  285. #pragma mark -
  286. #pragma mark Transaction methods
  287. - (void) runTransactionBlock:(fbt_transactionresult_mutabledata)block {
  288. [FValidation validateFrom:@"runTransactionBlock:" writablePath:self.path];
  289. [self runTransactionBlock:block andCompletionBlock:nil withLocalEvents:YES];
  290. }
  291. - (void) runTransactionBlock:(fbt_transactionresult_mutabledata)update andCompletionBlock:(fbt_void_nserror_bool_datasnapshot)completionBlock {
  292. [FValidation validateFrom:@"runTransactionBlock:andCompletionBlock:" writablePath:self.path];
  293. [self runTransactionBlock:update andCompletionBlock:completionBlock withLocalEvents:YES];
  294. }
  295. - (void) runTransactionBlock:(fbt_transactionresult_mutabledata)block andCompletionBlock:(fbt_void_nserror_bool_datasnapshot)completionBlock withLocalEvents:(BOOL)localEvents {
  296. [FValidation validateFrom:@"runTransactionBlock:andCompletionBlock:withLocalEvents:" writablePath:self.path];
  297. fbt_transactionresult_mutabledata updateCopy = [block copy];
  298. fbt_void_nserror_bool_datasnapshot onCompleteCopy = [completionBlock copy];
  299. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  300. [self.repo startTransactionOnPath:self.path update:updateCopy onComplete:onCompleteCopy withLocalEvents:localEvents];
  301. });
  302. }
  303. @end