FIRDatabaseQuery.m 33 KB

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