FIRDatabaseReference.m 15 KB

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