FIRDatabaseQuery.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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/FIRDatabaseQuery.h"
  17. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  18. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  19. #import "FirebaseDatabase/Sources/Core/FQueryParams.h"
  20. #import "FirebaseDatabase/Sources/Core/FQuerySpec.h"
  21. #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  22. #import "FirebaseDatabase/Sources/Core/View/FChildEventRegistration.h"
  23. #import "FirebaseDatabase/Sources/Core/View/FValueEventRegistration.h"
  24. #import "FirebaseDatabase/Sources/FKeyIndex.h"
  25. #import "FirebaseDatabase/Sources/FPathIndex.h"
  26. #import "FirebaseDatabase/Sources/FPriorityIndex.h"
  27. #import "FirebaseDatabase/Sources/FValueIndex.h"
  28. #import "FirebaseDatabase/Sources/Snapshot/FLeafNode.h"
  29. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  30. #import "FirebaseDatabase/Sources/Utilities/FNextPushId.h"
  31. #import "FirebaseDatabase/Sources/Utilities/FValidation.h"
  32. @implementation FIRDatabaseQuery
  33. @synthesize repo;
  34. @synthesize path;
  35. @synthesize queryParams;
  36. #define INVALID_QUERY_PARAM_ERROR @"InvalidQueryParameter"
  37. + (dispatch_queue_t)sharedQueue {
  38. // We use this shared queue across all of the FQueries so things happen FIFO
  39. // (as opposed to dispatch_get_global_queue(0, 0) which is concurrent)
  40. static dispatch_once_t pred;
  41. static dispatch_queue_t sharedDispatchQueue;
  42. dispatch_once(&pred, ^{
  43. sharedDispatchQueue = dispatch_queue_create("FirebaseWorker", NULL);
  44. });
  45. return sharedDispatchQueue;
  46. }
  47. - (id)initWithRepo:(FRepo *)theRepo path:(FPath *)thePath {
  48. return [self initWithRepo:theRepo
  49. path:thePath
  50. params:nil
  51. orderByCalled:NO
  52. priorityMethodCalled:NO];
  53. }
  54. - (id)initWithRepo:(FRepo *)theRepo
  55. path:(FPath *)thePath
  56. params:(FQueryParams *)theParams
  57. orderByCalled:(BOOL)orderByCalled
  58. priorityMethodCalled:(BOOL)priorityMethodCalled {
  59. self = [super init];
  60. if (self) {
  61. self.repo = theRepo;
  62. self.path = thePath;
  63. if (!theParams) {
  64. theParams = [FQueryParams defaultInstance];
  65. }
  66. if (![theParams isValid]) {
  67. @throw [[NSException alloc]
  68. initWithName:@"InvalidArgumentError"
  69. reason:@"Queries are limited to two constraints"
  70. userInfo:nil];
  71. }
  72. self.queryParams = theParams;
  73. self.orderByCalled = orderByCalled;
  74. self.priorityMethodCalled = priorityMethodCalled;
  75. }
  76. return self;
  77. }
  78. - (FQuerySpec *)querySpec {
  79. return [[FQuerySpec alloc] initWithPath:self.path params:self.queryParams];
  80. }
  81. - (void)validateQueryEndpointsForParams:(FQueryParams *)params {
  82. if ([params.index isEqual:[FKeyIndex keyIndex]]) {
  83. if ([params hasStart]) {
  84. if (params.indexStartKey != [FUtilities minName] &&
  85. params.indexStartKey != [FUtilities maxName]) {
  86. [NSException raise:INVALID_QUERY_PARAM_ERROR
  87. format:@"Can't use queryStartingAtValue:childKey: "
  88. @"or queryEqualTo:andChildKey: in "
  89. @"combination with queryOrderedByKey"];
  90. }
  91. if (![params.indexStartValue.val isKindOfClass:[NSString class]]) {
  92. [NSException
  93. raise:INVALID_QUERY_PARAM_ERROR
  94. format:
  95. @"Can't use queryStartingAtValue: with other types "
  96. @"than string in combination with queryOrderedByKey"];
  97. }
  98. }
  99. if ([params hasEnd]) {
  100. if (params.indexEndKey != [FUtilities maxName] &&
  101. params.indexEndKey != [FUtilities minName]) {
  102. [NSException raise:INVALID_QUERY_PARAM_ERROR
  103. format:@"Can't use queryEndingAtValue:childKey: or "
  104. @"queryEqualToValue:childKey: in "
  105. @"combination with queryOrderedByKey"];
  106. }
  107. if (![params.indexEndValue.val isKindOfClass:[NSString class]]) {
  108. [NSException
  109. raise:INVALID_QUERY_PARAM_ERROR
  110. format:
  111. @"Can't use queryEndingAtValue: with other types than "
  112. @"string in combination with queryOrderedByKey"];
  113. }
  114. }
  115. } else if ([params.index isEqual:[FPriorityIndex priorityIndex]]) {
  116. if (([params hasStart] &&
  117. ![FValidation validatePriorityValue:params.indexStartValue.val]) ||
  118. ([params hasEnd] &&
  119. ![FValidation validatePriorityValue:params.indexEndValue.val])) {
  120. [NSException
  121. raise:INVALID_QUERY_PARAM_ERROR
  122. format:@"When using queryOrderedByPriority, values provided to "
  123. @"queryStartingAtValue:, queryEndingAtValue:, or "
  124. @"queryEqualToValue: must be valid priorities."];
  125. }
  126. }
  127. }
  128. - (void)validateEqualToCall {
  129. if ([self.queryParams hasStart]) {
  130. [NSException
  131. raise:INVALID_QUERY_PARAM_ERROR
  132. format:
  133. @"Cannot combine queryEqualToValue: and queryStartingAtValue:"];
  134. }
  135. if ([self.queryParams hasEnd]) {
  136. [NSException
  137. raise:INVALID_QUERY_PARAM_ERROR
  138. format:
  139. @"Cannot combine queryEqualToValue: and queryEndingAtValue:"];
  140. }
  141. }
  142. - (void)validateNoPreviousOrderByCalled {
  143. if (self.orderByCalled) {
  144. [NSException raise:INVALID_QUERY_PARAM_ERROR
  145. format:@"Cannot use multiple queryOrderedBy calls!"];
  146. }
  147. }
  148. - (void)validateIndexValueType:(id)type fromMethod:(NSString *)method {
  149. if (type != nil && ![type isKindOfClass:[NSNumber class]] &&
  150. ![type isKindOfClass:[NSString class]] &&
  151. ![type isKindOfClass:[NSNull class]]) {
  152. [NSException raise:INVALID_QUERY_PARAM_ERROR
  153. format:@"You can only pass nil, NSString or NSNumber to %@",
  154. method];
  155. }
  156. }
  157. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue {
  158. return [self queryStartingAtInternal:startValue
  159. childKey:nil
  160. from:@"queryStartingAtValue:"
  161. priorityMethod:NO];
  162. }
  163. - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue
  164. childKey:(NSString *)childKey {
  165. if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]]) {
  166. @throw [[NSException alloc]
  167. initWithName:INVALID_QUERY_PARAM_ERROR
  168. reason:@"You must use queryStartingAtValue: instead of "
  169. @"queryStartingAtValue:childKey: when using "
  170. @"queryOrderedByKey:"
  171. userInfo:nil];
  172. }
  173. NSString *methodName = @"queryStartingAtValue:childKey:";
  174. if (childKey != nil) {
  175. [FValidation validateFrom:methodName validKey:childKey];
  176. }
  177. return [self queryStartingAtInternal:startValue
  178. childKey:childKey
  179. from:methodName
  180. priorityMethod:NO];
  181. }
  182. - (FIRDatabaseQuery *)queryStartingAfterValue:(id)startAfterValue {
  183. return [self queryStartingAfterValue:startAfterValue childKey:nil];
  184. }
  185. - (FIRDatabaseQuery *)queryStartingAfterValue:(id)startAfterValue
  186. childKey:(NSString *)childKey {
  187. if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]] &&
  188. childKey != nil) {
  189. @throw [[NSException alloc]
  190. initWithName:INVALID_QUERY_PARAM_ERROR
  191. reason:@"You must use queryStartingAfterValue: instead of "
  192. @"queryStartingAfterValue:childKey: when using "
  193. @"queryOrderedByKey:"
  194. userInfo:nil];
  195. }
  196. if (childKey == nil) {
  197. childKey = [FUtilities maxName];
  198. } else {
  199. childKey = [FNextPushId successor:childKey];
  200. NSLog(@"successor of child key %@", childKey);
  201. }
  202. NSString *methodName = @"queryStartingAfterValue:childKey:";
  203. if (childKey != nil && ![childKey isEqual:[FUtilities maxName]]) {
  204. [FValidation validateFrom:methodName validKey:childKey];
  205. }
  206. return [self queryStartingAtInternal:startAfterValue
  207. childKey:childKey
  208. from:methodName
  209. priorityMethod:NO];
  210. }
  211. - (FIRDatabaseQuery *)queryStartingAtInternal:(id<FNode>)startValue
  212. childKey:(NSString *)childKey
  213. from:(NSString *)methodName
  214. priorityMethod:(BOOL)priorityMethod {
  215. [self validateIndexValueType:startValue fromMethod:methodName];
  216. if ([self.queryParams hasStart]) {
  217. [NSException raise:INVALID_QUERY_PARAM_ERROR
  218. format:@"Can't call %@ after queryStartingAtValue or "
  219. @"queryEqualToValue was previously called",
  220. methodName];
  221. }
  222. id<FNode> startNode = [FSnapshotUtilities nodeFrom:startValue];
  223. FQueryParams *params = [self.queryParams startAt:startNode
  224. childKey:childKey];
  225. [self validateQueryEndpointsForParams:params];
  226. return [[FIRDatabaseQuery alloc]
  227. initWithRepo:self.repo
  228. path:self.path
  229. params:params
  230. orderByCalled:self.orderByCalled
  231. priorityMethodCalled:priorityMethod || self.priorityMethodCalled];
  232. }
  233. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue {
  234. return [self queryEndingAtInternal:endValue
  235. childKey:nil
  236. from:@"queryEndingAtValue:"
  237. priorityMethod:NO];
  238. }
  239. - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue
  240. childKey:(NSString *)childKey {
  241. if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]]) {
  242. @throw [[NSException alloc]
  243. initWithName:INVALID_QUERY_PARAM_ERROR
  244. reason:@"You must use queryEndingAtValue: instead of "
  245. @"queryEndingAtValue:childKey: when using "
  246. @"queryOrderedByKey:"
  247. userInfo:nil];
  248. }
  249. NSString *methodName = @"queryEndingAtValue:childKey:";
  250. if (childKey != nil) {
  251. [FValidation validateFrom:methodName validKey:childKey];
  252. }
  253. return [self queryEndingAtInternal:endValue
  254. childKey:childKey
  255. from:methodName
  256. priorityMethod:NO];
  257. }
  258. - (FIRDatabaseQuery *)queryEndingBeforeValue:(id)endValue {
  259. return [self queryEndingBeforeValue:endValue childKey:nil];
  260. }
  261. - (FIRDatabaseQuery *)queryEndingBeforeValue:(id)endValue
  262. childKey:(NSString *)childKey {
  263. if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]] &&
  264. childKey != nil) {
  265. @throw [[NSException alloc]
  266. initWithName:INVALID_QUERY_PARAM_ERROR
  267. reason:@"You must use queryEndingBeforeValue: instead of "
  268. @"queryEndingBeforeValue:childKey: when using "
  269. @"queryOrderedByKey:"
  270. userInfo:nil];
  271. }
  272. if (childKey == nil) {
  273. childKey = [FUtilities minName];
  274. } else {
  275. childKey = [FNextPushId predecessor:childKey];
  276. }
  277. NSString *methodName = @"queryEndingBeforeValue:childKey:";
  278. if (childKey != nil && ![childKey isEqual:[FUtilities minName]]) {
  279. [FValidation validateFrom:methodName validKey:childKey];
  280. }
  281. return [self queryEndingAtInternal:endValue
  282. childKey:childKey
  283. from:methodName
  284. priorityMethod:NO];
  285. }
  286. - (FIRDatabaseQuery *)queryEndingAtInternal:(id)endValue
  287. childKey:(NSString *)childKey
  288. from:(NSString *)methodName
  289. priorityMethod:(BOOL)priorityMethod {
  290. [self validateIndexValueType:endValue fromMethod:methodName];
  291. if ([self.queryParams hasEnd]) {
  292. [NSException raise:INVALID_QUERY_PARAM_ERROR
  293. format:@"Can't call %@ after queryEndingAtValue or "
  294. @"queryEqualToValue was previously called",
  295. methodName];
  296. }
  297. id<FNode> endNode = [FSnapshotUtilities nodeFrom:endValue];
  298. FQueryParams *params = [self.queryParams endAt:endNode childKey:childKey];
  299. [self validateQueryEndpointsForParams:params];
  300. return [[FIRDatabaseQuery alloc]
  301. initWithRepo:self.repo
  302. path:self.path
  303. params:params
  304. orderByCalled:self.orderByCalled
  305. priorityMethodCalled:priorityMethod || self.priorityMethodCalled];
  306. }
  307. - (FIRDatabaseQuery *)queryEqualToValue:(id)value {
  308. return [self queryEqualToInternal:value
  309. childKey:nil
  310. from:@"queryEqualToValue:"
  311. priorityMethod:NO];
  312. }
  313. - (FIRDatabaseQuery *)queryEqualToValue:(id)value
  314. childKey:(NSString *)childKey {
  315. if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]]) {
  316. @throw [[NSException alloc]
  317. initWithName:INVALID_QUERY_PARAM_ERROR
  318. reason:@"You must use queryEqualToValue: instead of "
  319. @"queryEqualTo:childKey: when using queryOrderedByKey:"
  320. userInfo:nil];
  321. }
  322. return [self queryEqualToInternal:value
  323. childKey:childKey
  324. from:@"queryEqualToValue:childKey:"
  325. priorityMethod:NO];
  326. }
  327. - (FIRDatabaseQuery *)queryEqualToInternal:(id)value
  328. childKey:(NSString *)childKey
  329. from:(NSString *)methodName
  330. priorityMethod:(BOOL)priorityMethod {
  331. [self validateIndexValueType:value fromMethod:methodName];
  332. if (childKey != nil) {
  333. [FValidation validateFrom:methodName validKey:childKey];
  334. }
  335. if ([self.queryParams hasEnd] || [self.queryParams hasStart]) {
  336. [NSException
  337. raise:INVALID_QUERY_PARAM_ERROR
  338. format:
  339. @"Can't call %@ after queryStartingAtValue, queryEndingAtValue "
  340. @"or queryEqualToValue was previously called",
  341. methodName];
  342. }
  343. id<FNode> node = [FSnapshotUtilities nodeFrom:value];
  344. FQueryParams *params = [[self.queryParams startAt:node
  345. childKey:childKey] endAt:node
  346. childKey:childKey];
  347. [self validateQueryEndpointsForParams:params];
  348. return [[FIRDatabaseQuery alloc]
  349. initWithRepo:self.repo
  350. path:self.path
  351. params:params
  352. orderByCalled:self.orderByCalled
  353. priorityMethodCalled:priorityMethod || self.priorityMethodCalled];
  354. }
  355. - (void)validateLimitRange:(NSUInteger)limit {
  356. // No need to check for negative ranges, since limit is unsigned
  357. if (limit == 0) {
  358. [NSException raise:INVALID_QUERY_PARAM_ERROR
  359. format:@"Limit can't be zero"];
  360. }
  361. if (limit >= 1ul << 31) {
  362. [NSException raise:INVALID_QUERY_PARAM_ERROR
  363. format:@"Limit must be less than 2,147,483,648"];
  364. }
  365. }
  366. - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit {
  367. if (self.queryParams.limitSet) {
  368. [NSException raise:INVALID_QUERY_PARAM_ERROR
  369. format:@"Can't call queryLimitedToFirst: if a limit was "
  370. @"previously set"];
  371. }
  372. [self validateLimitRange:limit];
  373. FQueryParams *params = [self.queryParams limitToFirst:limit];
  374. return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
  375. path:self.path
  376. params:params
  377. orderByCalled:self.orderByCalled
  378. priorityMethodCalled:self.priorityMethodCalled];
  379. }
  380. - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit {
  381. if (self.queryParams.limitSet) {
  382. [NSException raise:INVALID_QUERY_PARAM_ERROR
  383. format:@"Can't call queryLimitedToLast: if a limit was "
  384. @"previously set"];
  385. }
  386. [self validateLimitRange:limit];
  387. FQueryParams *params = [self.queryParams limitToLast:limit];
  388. return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
  389. path:self.path
  390. params:params
  391. orderByCalled:self.orderByCalled
  392. priorityMethodCalled:self.priorityMethodCalled];
  393. }
  394. - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)indexPathString {
  395. if ([indexPathString isEqualToString:@"$key"] ||
  396. [indexPathString isEqualToString:@".key"]) {
  397. @throw [[NSException alloc]
  398. initWithName:INVALID_QUERY_PARAM_ERROR
  399. reason:[NSString stringWithFormat:
  400. @"(queryOrderedByChild:) %@ is invalid. "
  401. @" Use queryOrderedByKey: instead.",
  402. indexPathString]
  403. userInfo:nil];
  404. } else if ([indexPathString isEqualToString:@"$priority"] ||
  405. [indexPathString isEqualToString:@".priority"]) {
  406. @throw [[NSException alloc]
  407. initWithName:INVALID_QUERY_PARAM_ERROR
  408. reason:[NSString stringWithFormat:
  409. @"(queryOrderedByChild:) %@ is invalid. "
  410. @" Use queryOrderedByPriority: instead.",
  411. indexPathString]
  412. userInfo:nil];
  413. } else if ([indexPathString isEqualToString:@"$value"] ||
  414. [indexPathString isEqualToString:@".value"]) {
  415. @throw [[NSException alloc]
  416. initWithName:INVALID_QUERY_PARAM_ERROR
  417. reason:[NSString stringWithFormat:
  418. @"(queryOrderedByChild:) %@ is invalid. "
  419. @" Use queryOrderedByValue: instead.",
  420. indexPathString]
  421. userInfo:nil];
  422. }
  423. [self validateNoPreviousOrderByCalled];
  424. [FValidation validateFrom:@"queryOrderedByChild:"
  425. validPathString:indexPathString];
  426. FPath *indexPath = [FPath pathWithString:indexPathString];
  427. if (indexPath.isEmpty) {
  428. @throw [[NSException alloc]
  429. initWithName:INVALID_QUERY_PARAM_ERROR
  430. reason:[NSString
  431. stringWithFormat:@"(queryOrderedByChild:) with an "
  432. @"empty path is invalid. Use "
  433. @"queryOrderedByValue: instead."]
  434. userInfo:nil];
  435. }
  436. id<FIndex> index = [[FPathIndex alloc] initWithPath:indexPath];
  437. FQueryParams *params = [self.queryParams orderBy:index];
  438. [self validateQueryEndpointsForParams:params];
  439. return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
  440. path:self.path
  441. params:params
  442. orderByCalled:YES
  443. priorityMethodCalled:self.priorityMethodCalled];
  444. }
  445. - (FIRDatabaseQuery *)queryOrderedByKey {
  446. [self validateNoPreviousOrderByCalled];
  447. FQueryParams *params = [self.queryParams orderBy:[FKeyIndex keyIndex]];
  448. [self validateQueryEndpointsForParams:params];
  449. return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
  450. path:self.path
  451. params:params
  452. orderByCalled:YES
  453. priorityMethodCalled:self.priorityMethodCalled];
  454. }
  455. - (FIRDatabaseQuery *)queryOrderedByValue {
  456. [self validateNoPreviousOrderByCalled];
  457. FQueryParams *params = [self.queryParams orderBy:[FValueIndex valueIndex]];
  458. return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
  459. path:self.path
  460. params:params
  461. orderByCalled:YES
  462. priorityMethodCalled:self.priorityMethodCalled];
  463. }
  464. - (FIRDatabaseQuery *)queryOrderedByPriority {
  465. [self validateNoPreviousOrderByCalled];
  466. FQueryParams *params =
  467. [self.queryParams orderBy:[FPriorityIndex priorityIndex]];
  468. return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
  469. path:self.path
  470. params:params
  471. orderByCalled:YES
  472. priorityMethodCalled:self.priorityMethodCalled];
  473. }
  474. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  475. withBlock:(void (^)(FIRDataSnapshot *))block {
  476. [FValidation validateFrom:@"observeEventType:withBlock:"
  477. knownEventType:eventType];
  478. return [self observeEventType:eventType
  479. withBlock:block
  480. withCancelBlock:nil];
  481. }
  482. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  483. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
  484. [FValidation
  485. validateFrom:@"observeEventType:andPreviousSiblingKeyWithBlock:"
  486. knownEventType:eventType];
  487. return [self observeEventType:eventType
  488. andPreviousSiblingKeyWithBlock:block
  489. withCancelBlock:nil];
  490. }
  491. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  492. withBlock:(fbt_void_datasnapshot)block
  493. withCancelBlock:(fbt_void_nserror)cancelBlock {
  494. [FValidation validateFrom:@"observeEventType:withBlock:withCancelBlock:"
  495. knownEventType:eventType];
  496. if (eventType == FIRDataEventTypeValue) {
  497. // Handle FIRDataEventTypeValue specially because they shouldn't have
  498. // prevName callbacks
  499. NSUInteger handle = [[FUtilities LUIDGenerator] integerValue];
  500. [self observeValueEventWithHandle:handle
  501. withBlock:block
  502. cancelCallback:cancelBlock];
  503. return handle;
  504. } else {
  505. // Wrap up the userCallback so we can treat everything as a callback
  506. // that has a prevName
  507. fbt_void_datasnapshot userCallback = [block copy];
  508. return [self observeEventType:eventType
  509. andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot,
  510. NSString *prevName) {
  511. if (userCallback != nil) {
  512. userCallback(snapshot);
  513. }
  514. }
  515. withCancelBlock:cancelBlock];
  516. }
  517. }
  518. - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
  519. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block
  520. withCancelBlock:(fbt_void_nserror)cancelBlock {
  521. [FValidation validateFrom:@"observeEventType:"
  522. @"andPreviousSiblingKeyWithBlock:withCancelBlock:"
  523. knownEventType:eventType];
  524. if (eventType == FIRDataEventTypeValue) {
  525. // TODO: This gets hit by observeSingleEventOfType. Need to fix.
  526. /*
  527. @throw [[NSException alloc] initWithName:@"InvalidEventTypeForObserver"
  528. reason:@"(observeEventType:andPreviousSiblingKeyWithBlock:withCancelBlock:)
  529. Cannot use
  530. observeEventType:andPreviousSiblingKeyWithBlock:withCancelBlock: with
  531. FIRDataEventTypeValue. Use observeEventType:withBlock:withCancelBlock:
  532. instead." userInfo:nil];
  533. */
  534. }
  535. NSUInteger handle = [[FUtilities LUIDGenerator] integerValue];
  536. NSDictionary *callbacks =
  537. @{[NSNumber numberWithInteger:eventType] : [block copy]};
  538. [self observeChildEventWithHandle:handle
  539. withCallbacks:callbacks
  540. cancelCallback:cancelBlock];
  541. return handle;
  542. }
  543. // If we want to distinguish between value event listeners and child event
  544. // listeners, like in the Java client, we can consider exporting this. If we do,
  545. // add argument validation. Otherwise, arguments are validated in the
  546. // public-facing portions of the API. Also, move the FIRDatabaseHandle logic.
  547. - (void)observeValueEventWithHandle:(FIRDatabaseHandle)handle
  548. withBlock:(fbt_void_datasnapshot)block
  549. cancelCallback:(fbt_void_nserror)cancelBlock {
  550. // Note that we don't need to copy the callbacks here, FEventRegistration
  551. // callback properties set to copy
  552. FValueEventRegistration *registration =
  553. [[FValueEventRegistration alloc] initWithRepo:self.repo
  554. handle:handle
  555. callback:block
  556. cancelCallback:cancelBlock];
  557. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  558. [self.repo addEventRegistration:registration forQuery:self.querySpec];
  559. });
  560. }
  561. // Note: as with the above method, we may wish to expose this at some point.
  562. - (void)observeChildEventWithHandle:(FIRDatabaseHandle)handle
  563. withCallbacks:(NSDictionary *)callbacks
  564. cancelCallback:(fbt_void_nserror)cancelBlock {
  565. // Note that we don't need to copy the callbacks here, FEventRegistration
  566. // callback properties set to copy
  567. FChildEventRegistration *registration =
  568. [[FChildEventRegistration alloc] initWithRepo:self.repo
  569. handle:handle
  570. callbacks:callbacks
  571. cancelCallback:cancelBlock];
  572. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  573. [self.repo addEventRegistration:registration forQuery:self.querySpec];
  574. });
  575. }
  576. - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle {
  577. FValueEventRegistration *event =
  578. [[FValueEventRegistration alloc] initWithRepo:self.repo
  579. handle:handle
  580. callback:nil
  581. cancelCallback:nil];
  582. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  583. [self.repo removeEventRegistration:event forQuery:self.querySpec];
  584. });
  585. }
  586. - (void)removeAllObservers {
  587. [self removeObserverWithHandle:NSNotFound];
  588. }
  589. - (void)keepSynced:(BOOL)keepSynced {
  590. if ([self.path.getFront isEqualToString:kDotInfoPrefix]) {
  591. [NSException raise:NSInvalidArgumentException
  592. format:@"Can't keep query on .info tree synced (this "
  593. @"already is the case)."];
  594. }
  595. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  596. [self.repo keepQuery:self.querySpec synced:keepSynced];
  597. });
  598. }
  599. - (void)getDataWithCompletionBlock:(void (^)(NSError *__nullable error,
  600. FIRDataSnapshot *snapshot))block {
  601. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  602. [self.repo getData:self withCompletionBlock:block];
  603. });
  604. }
  605. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  606. withBlock:(fbt_void_datasnapshot)block {
  607. [self observeSingleEventOfType:eventType
  608. withBlock:block
  609. withCancelBlock:nil];
  610. }
  611. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  612. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
  613. [self observeSingleEventOfType:eventType
  614. andPreviousSiblingKeyWithBlock:block
  615. withCancelBlock:nil];
  616. }
  617. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  618. withBlock:(fbt_void_datasnapshot)block
  619. withCancelBlock:(fbt_void_nserror)cancelBlock {
  620. // XXX: user reported memory leak in method
  621. // "When you copy a block, any references to other blocks from within that
  622. // block are copied if necessary—an entire tree may be copied (from the
  623. // top). If you have block variables and you reference a block from within
  624. // the block, that block will be copied."
  625. // http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html#//apple_ref/doc/uid/TP40007502-CH6-SW1
  626. // So... we don't need to do this since inside the on: we copy this block
  627. // off the stack to the heap.
  628. // __block fbt_void_datasnapshot userCallback = [callback copy];
  629. [self observeSingleEventOfType:eventType
  630. andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot,
  631. NSString *prevName) {
  632. if (block != nil) {
  633. block(snapshot);
  634. }
  635. }
  636. withCancelBlock:cancelBlock];
  637. }
  638. /**
  639. * Attaches a listener, waits for the first event, and then removes the listener
  640. */
  641. - (void)observeSingleEventOfType:(FIRDataEventType)eventType
  642. andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block
  643. withCancelBlock:(fbt_void_nserror)cancelBlock {
  644. // XXX: user reported memory leak in method
  645. // "When you copy a block, any references to other blocks from within that
  646. // block are copied if necessary—an entire tree may be copied (from the
  647. // top). If you have block variables and you reference a block from within
  648. // the block, that block will be copied."
  649. // http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html#//apple_ref/doc/uid/TP40007502-CH6-SW1
  650. // So... we don't need to do this since inside the on: we copy this block
  651. // off the stack to the heap.
  652. // __block fbt_void_datasnapshot userCallback = [callback copy];
  653. __block FIRDatabaseHandle handle;
  654. __block BOOL firstCall = YES;
  655. fbt_void_datasnapshot_nsstring callback = [block copy];
  656. fbt_void_datasnapshot_nsstring wrappedCallback =
  657. ^(FIRDataSnapshot *snap, NSString *prevName) {
  658. if (firstCall) {
  659. firstCall = NO;
  660. [self removeObserverWithHandle:handle];
  661. callback(snap, prevName);
  662. }
  663. };
  664. fbt_void_nserror cancelCallback = [cancelBlock copy];
  665. handle = [self observeEventType:eventType
  666. andPreviousSiblingKeyWithBlock:wrappedCallback
  667. withCancelBlock:^(NSError *error) {
  668. [self removeObserverWithHandle:handle];
  669. if (cancelCallback) {
  670. cancelCallback(error);
  671. }
  672. }];
  673. }
  674. - (NSString *)description {
  675. return [NSString
  676. stringWithFormat:@"(%@ %@)", self.path, self.queryParams.description];
  677. }
  678. - (FIRDatabaseReference *)ref {
  679. return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:self.path];
  680. }
  681. @end