FIRDatabaseQuery.m 32 KB

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