| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- /*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabaseQuery.h"
- #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
- #import "FirebaseDatabase/Sources/Constants/FConstants.h"
- #import "FirebaseDatabase/Sources/Core/FQueryParams.h"
- #import "FirebaseDatabase/Sources/Core/FQuerySpec.h"
- #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
- #import "FirebaseDatabase/Sources/Core/View/FChildEventRegistration.h"
- #import "FirebaseDatabase/Sources/Core/View/FValueEventRegistration.h"
- #import "FirebaseDatabase/Sources/FKeyIndex.h"
- #import "FirebaseDatabase/Sources/FPathIndex.h"
- #import "FirebaseDatabase/Sources/FPriorityIndex.h"
- #import "FirebaseDatabase/Sources/FValueIndex.h"
- #import "FirebaseDatabase/Sources/Snapshot/FLeafNode.h"
- #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
- #import "FirebaseDatabase/Sources/Utilities/FValidation.h"
- @implementation FIRDatabaseQuery
- @synthesize repo;
- @synthesize path;
- @synthesize queryParams;
- #define INVALID_QUERY_PARAM_ERROR @"InvalidQueryParameter"
- + (dispatch_queue_t)sharedQueue {
- // We use this shared queue across all of the FQueries so things happen FIFO
- // (as opposed to dispatch_get_global_queue(0, 0) which is concurrent)
- static dispatch_once_t pred;
- static dispatch_queue_t sharedDispatchQueue;
- dispatch_once(&pred, ^{
- sharedDispatchQueue = dispatch_queue_create("FirebaseWorker", NULL);
- });
- return sharedDispatchQueue;
- }
- - (id)initWithRepo:(FRepo *)theRepo path:(FPath *)thePath {
- return [self initWithRepo:theRepo
- path:thePath
- params:nil
- orderByCalled:NO
- priorityMethodCalled:NO];
- }
- - (id)initWithRepo:(FRepo *)theRepo
- path:(FPath *)thePath
- params:(FQueryParams *)theParams
- orderByCalled:(BOOL)orderByCalled
- priorityMethodCalled:(BOOL)priorityMethodCalled {
- self = [super init];
- if (self) {
- self.repo = theRepo;
- self.path = thePath;
- if (!theParams) {
- theParams = [FQueryParams defaultInstance];
- }
- if (![theParams isValid]) {
- @throw [[NSException alloc]
- initWithName:@"InvalidArgumentError"
- reason:@"Queries are limited to two constraints"
- userInfo:nil];
- }
- self.queryParams = theParams;
- self.orderByCalled = orderByCalled;
- self.priorityMethodCalled = priorityMethodCalled;
- }
- return self;
- }
- - (FQuerySpec *)querySpec {
- return [[FQuerySpec alloc] initWithPath:self.path params:self.queryParams];
- }
- - (void)validateQueryEndpointsForParams:(FQueryParams *)params {
- if ([params.index isEqual:[FKeyIndex keyIndex]]) {
- if ([params hasStart]) {
- if (params.indexStartKey != [FUtilities minName]) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Can't use queryStartingAtValue:childKey: "
- @"or queryEqualTo:andChildKey: in "
- @"combination with queryOrderedByKey"];
- }
- if (![params.indexStartValue.val isKindOfClass:[NSString class]]) {
- [NSException
- raise:INVALID_QUERY_PARAM_ERROR
- format:
- @"Can't use queryStartingAtValue: with other types "
- @"than string in combination with queryOrderedByKey"];
- }
- }
- if ([params hasEnd]) {
- if (params.indexEndKey != [FUtilities maxName]) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Can't use queryEndingAtValue:childKey: or "
- @"queryEqualToValue:childKey: in "
- @"combination with queryOrderedByKey"];
- }
- if (![params.indexEndValue.val isKindOfClass:[NSString class]]) {
- [NSException
- raise:INVALID_QUERY_PARAM_ERROR
- format:
- @"Can't use queryEndingAtValue: with other types than "
- @"string in combination with queryOrderedByKey"];
- }
- }
- } else if ([params.index isEqual:[FPriorityIndex priorityIndex]]) {
- if (([params hasStart] &&
- ![FValidation validatePriorityValue:params.indexStartValue.val]) ||
- ([params hasEnd] &&
- ![FValidation validatePriorityValue:params.indexEndValue.val])) {
- [NSException
- raise:INVALID_QUERY_PARAM_ERROR
- format:@"When using queryOrderedByPriority, values provided to "
- @"queryStartingAtValue:, queryEndingAtValue:, or "
- @"queryEqualToValue: must be valid priorities."];
- }
- }
- }
- - (void)validateEqualToCall {
- if ([self.queryParams hasStart]) {
- [NSException
- raise:INVALID_QUERY_PARAM_ERROR
- format:
- @"Cannot combine queryEqualToValue: and queryStartingAtValue:"];
- }
- if ([self.queryParams hasEnd]) {
- [NSException
- raise:INVALID_QUERY_PARAM_ERROR
- format:
- @"Cannot combine queryEqualToValue: and queryEndingAtValue:"];
- }
- }
- - (void)validateNoPreviousOrderByCalled {
- if (self.orderByCalled) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Cannot use multiple queryOrderedBy calls!"];
- }
- }
- - (void)validateIndexValueType:(id)type fromMethod:(NSString *)method {
- if (type != nil && ![type isKindOfClass:[NSNumber class]] &&
- ![type isKindOfClass:[NSString class]] &&
- ![type isKindOfClass:[NSNull class]]) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"You can only pass nil, NSString or NSNumber to %@",
- method];
- }
- }
- - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue {
- return [self queryStartingAtInternal:startValue
- childKey:nil
- from:@"queryStartingAtValue:"
- priorityMethod:NO];
- }
- - (FIRDatabaseQuery *)queryStartingAtValue:(id)startValue
- childKey:(NSString *)childKey {
- if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]]) {
- @throw [[NSException alloc]
- initWithName:INVALID_QUERY_PARAM_ERROR
- reason:@"You must use queryStartingAtValue: instead of "
- @"queryStartingAtValue:childKey: when using "
- @"queryOrderedByKey:"
- userInfo:nil];
- }
- return [self queryStartingAtInternal:startValue
- childKey:childKey
- from:@"queryStartingAtValue:childKey:"
- priorityMethod:NO];
- }
- - (FIRDatabaseQuery *)queryStartingAtInternal:(id<FNode>)startValue
- childKey:(NSString *)childKey
- from:(NSString *)methodName
- priorityMethod:(BOOL)priorityMethod {
- [self validateIndexValueType:startValue fromMethod:methodName];
- if (childKey != nil) {
- [FValidation validateFrom:methodName validKey:childKey];
- }
- if ([self.queryParams hasStart]) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Can't call %@ after queryStartingAtValue or "
- @"queryEqualToValue was previously called",
- methodName];
- }
- id<FNode> startNode = [FSnapshotUtilities nodeFrom:startValue];
- FQueryParams *params = [self.queryParams startAt:startNode
- childKey:childKey];
- [self validateQueryEndpointsForParams:params];
- return [[FIRDatabaseQuery alloc]
- initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:self.orderByCalled
- priorityMethodCalled:priorityMethod || self.priorityMethodCalled];
- }
- - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue {
- return [self queryEndingAtInternal:endValue
- childKey:nil
- from:@"queryEndingAtValue:"
- priorityMethod:NO];
- }
- - (FIRDatabaseQuery *)queryEndingAtValue:(id)endValue
- childKey:(NSString *)childKey {
- if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]]) {
- @throw [[NSException alloc]
- initWithName:INVALID_QUERY_PARAM_ERROR
- reason:@"You must use queryEndingAtValue: instead of "
- @"queryEndingAtValue:childKey: when using "
- @"queryOrderedByKey:"
- userInfo:nil];
- }
- return [self queryEndingAtInternal:endValue
- childKey:childKey
- from:@"queryEndingAtValue:childKey:"
- priorityMethod:NO];
- }
- - (FIRDatabaseQuery *)queryEndingAtInternal:(id)endValue
- childKey:(NSString *)childKey
- from:(NSString *)methodName
- priorityMethod:(BOOL)priorityMethod {
- [self validateIndexValueType:endValue fromMethod:methodName];
- if (childKey != nil) {
- [FValidation validateFrom:methodName validKey:childKey];
- }
- if ([self.queryParams hasEnd]) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Can't call %@ after queryEndingAtValue or "
- @"queryEqualToValue was previously called",
- methodName];
- }
- id<FNode> endNode = [FSnapshotUtilities nodeFrom:endValue];
- FQueryParams *params = [self.queryParams endAt:endNode childKey:childKey];
- [self validateQueryEndpointsForParams:params];
- return [[FIRDatabaseQuery alloc]
- initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:self.orderByCalled
- priorityMethodCalled:priorityMethod || self.priorityMethodCalled];
- }
- - (FIRDatabaseQuery *)queryEqualToValue:(id)value {
- return [self queryEqualToInternal:value
- childKey:nil
- from:@"queryEqualToValue:"
- priorityMethod:NO];
- }
- - (FIRDatabaseQuery *)queryEqualToValue:(id)value
- childKey:(NSString *)childKey {
- if ([self.queryParams.index isEqual:[FKeyIndex keyIndex]]) {
- @throw [[NSException alloc]
- initWithName:INVALID_QUERY_PARAM_ERROR
- reason:@"You must use queryEqualToValue: instead of "
- @"queryEqualTo:childKey: when using queryOrderedByKey:"
- userInfo:nil];
- }
- return [self queryEqualToInternal:value
- childKey:childKey
- from:@"queryEqualToValue:childKey:"
- priorityMethod:NO];
- }
- - (FIRDatabaseQuery *)queryEqualToInternal:(id)value
- childKey:(NSString *)childKey
- from:(NSString *)methodName
- priorityMethod:(BOOL)priorityMethod {
- [self validateIndexValueType:value fromMethod:methodName];
- if (childKey != nil) {
- [FValidation validateFrom:methodName validKey:childKey];
- }
- if ([self.queryParams hasEnd] || [self.queryParams hasStart]) {
- [NSException
- raise:INVALID_QUERY_PARAM_ERROR
- format:
- @"Can't call %@ after queryStartingAtValue, queryEndingAtValue "
- @"or queryEqualToValue was previously called",
- methodName];
- }
- id<FNode> node = [FSnapshotUtilities nodeFrom:value];
- FQueryParams *params = [[self.queryParams startAt:node
- childKey:childKey] endAt:node
- childKey:childKey];
- [self validateQueryEndpointsForParams:params];
- return [[FIRDatabaseQuery alloc]
- initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:self.orderByCalled
- priorityMethodCalled:priorityMethod || self.priorityMethodCalled];
- }
- - (void)validateLimitRange:(NSUInteger)limit {
- // No need to check for negative ranges, since limit is unsigned
- if (limit == 0) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Limit can't be zero"];
- }
- if (limit >= 1ul << 31) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Limit must be less than 2,147,483,648"];
- }
- }
- - (FIRDatabaseQuery *)queryLimitedToFirst:(NSUInteger)limit {
- if (self.queryParams.limitSet) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Can't call queryLimitedToFirst: if a limit was "
- @"previously set"];
- }
- [self validateLimitRange:limit];
- FQueryParams *params = [self.queryParams limitToFirst:limit];
- return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:self.orderByCalled
- priorityMethodCalled:self.priorityMethodCalled];
- }
- - (FIRDatabaseQuery *)queryLimitedToLast:(NSUInteger)limit {
- if (self.queryParams.limitSet) {
- [NSException raise:INVALID_QUERY_PARAM_ERROR
- format:@"Can't call queryLimitedToLast: if a limit was "
- @"previously set"];
- }
- [self validateLimitRange:limit];
- FQueryParams *params = [self.queryParams limitToLast:limit];
- return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:self.orderByCalled
- priorityMethodCalled:self.priorityMethodCalled];
- }
- - (FIRDatabaseQuery *)queryOrderedByChild:(NSString *)indexPathString {
- if ([indexPathString isEqualToString:@"$key"] ||
- [indexPathString isEqualToString:@".key"]) {
- @throw [[NSException alloc]
- initWithName:INVALID_QUERY_PARAM_ERROR
- reason:[NSString stringWithFormat:
- @"(queryOrderedByChild:) %@ is invalid. "
- @" Use queryOrderedByKey: instead.",
- indexPathString]
- userInfo:nil];
- } else if ([indexPathString isEqualToString:@"$priority"] ||
- [indexPathString isEqualToString:@".priority"]) {
- @throw [[NSException alloc]
- initWithName:INVALID_QUERY_PARAM_ERROR
- reason:[NSString stringWithFormat:
- @"(queryOrderedByChild:) %@ is invalid. "
- @" Use queryOrderedByPriority: instead.",
- indexPathString]
- userInfo:nil];
- } else if ([indexPathString isEqualToString:@"$value"] ||
- [indexPathString isEqualToString:@".value"]) {
- @throw [[NSException alloc]
- initWithName:INVALID_QUERY_PARAM_ERROR
- reason:[NSString stringWithFormat:
- @"(queryOrderedByChild:) %@ is invalid. "
- @" Use queryOrderedByValue: instead.",
- indexPathString]
- userInfo:nil];
- }
- [self validateNoPreviousOrderByCalled];
- [FValidation validateFrom:@"queryOrderedByChild:"
- validPathString:indexPathString];
- FPath *indexPath = [FPath pathWithString:indexPathString];
- if (indexPath.isEmpty) {
- @throw [[NSException alloc]
- initWithName:INVALID_QUERY_PARAM_ERROR
- reason:[NSString
- stringWithFormat:@"(queryOrderedByChild:) with an "
- @"empty path is invalid. Use "
- @"queryOrderedByValue: instead."]
- userInfo:nil];
- }
- id<FIndex> index = [[FPathIndex alloc] initWithPath:indexPath];
- FQueryParams *params = [self.queryParams orderBy:index];
- [self validateQueryEndpointsForParams:params];
- return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:YES
- priorityMethodCalled:self.priorityMethodCalled];
- }
- - (FIRDatabaseQuery *)queryOrderedByKey {
- [self validateNoPreviousOrderByCalled];
- FQueryParams *params = [self.queryParams orderBy:[FKeyIndex keyIndex]];
- [self validateQueryEndpointsForParams:params];
- return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:YES
- priorityMethodCalled:self.priorityMethodCalled];
- }
- - (FIRDatabaseQuery *)queryOrderedByValue {
- [self validateNoPreviousOrderByCalled];
- FQueryParams *params = [self.queryParams orderBy:[FValueIndex valueIndex]];
- return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:YES
- priorityMethodCalled:self.priorityMethodCalled];
- }
- - (FIRDatabaseQuery *)queryOrderedByPriority {
- [self validateNoPreviousOrderByCalled];
- FQueryParams *params =
- [self.queryParams orderBy:[FPriorityIndex priorityIndex]];
- return [[FIRDatabaseQuery alloc] initWithRepo:self.repo
- path:self.path
- params:params
- orderByCalled:YES
- priorityMethodCalled:self.priorityMethodCalled];
- }
- - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
- withBlock:(void (^)(FIRDataSnapshot *))block {
- [FValidation validateFrom:@"observeEventType:withBlock:"
- knownEventType:eventType];
- return [self observeEventType:eventType
- withBlock:block
- withCancelBlock:nil];
- }
- - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
- andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
- [FValidation
- validateFrom:@"observeEventType:andPreviousSiblingKeyWithBlock:"
- knownEventType:eventType];
- return [self observeEventType:eventType
- andPreviousSiblingKeyWithBlock:block
- withCancelBlock:nil];
- }
- - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
- withBlock:(fbt_void_datasnapshot)block
- withCancelBlock:(fbt_void_nserror)cancelBlock {
- [FValidation validateFrom:@"observeEventType:withBlock:withCancelBlock:"
- knownEventType:eventType];
- if (eventType == FIRDataEventTypeValue) {
- // Handle FIRDataEventTypeValue specially because they shouldn't have
- // prevName callbacks
- NSUInteger handle = [[FUtilities LUIDGenerator] integerValue];
- [self observeValueEventWithHandle:handle
- withBlock:block
- cancelCallback:cancelBlock];
- return handle;
- } else {
- // Wrap up the userCallback so we can treat everything as a callback
- // that has a prevName
- fbt_void_datasnapshot userCallback = [block copy];
- return [self observeEventType:eventType
- andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot,
- NSString *prevName) {
- if (userCallback != nil) {
- userCallback(snapshot);
- }
- }
- withCancelBlock:cancelBlock];
- }
- }
- - (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
- andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block
- withCancelBlock:(fbt_void_nserror)cancelBlock {
- [FValidation validateFrom:@"observeEventType:"
- @"andPreviousSiblingKeyWithBlock:withCancelBlock:"
- knownEventType:eventType];
- if (eventType == FIRDataEventTypeValue) {
- // TODO: This gets hit by observeSingleEventOfType. Need to fix.
- /*
- @throw [[NSException alloc] initWithName:@"InvalidEventTypeForObserver"
- reason:@"(observeEventType:andPreviousSiblingKeyWithBlock:withCancelBlock:)
- Cannot use
- observeEventType:andPreviousSiblingKeyWithBlock:withCancelBlock: with
- FIRDataEventTypeValue. Use observeEventType:withBlock:withCancelBlock:
- instead." userInfo:nil];
- */
- }
- NSUInteger handle = [[FUtilities LUIDGenerator] integerValue];
- NSDictionary *callbacks =
- @{[NSNumber numberWithInteger:eventType] : [block copy]};
- [self observeChildEventWithHandle:handle
- withCallbacks:callbacks
- cancelCallback:cancelBlock];
- return handle;
- }
- // If we want to distinguish between value event listeners and child event
- // listeners, like in the Java client, we can consider exporting this. If we do,
- // add argument validation. Otherwise, arguments are validated in the
- // public-facing portions of the API. Also, move the FIRDatabaseHandle logic.
- - (void)observeValueEventWithHandle:(FIRDatabaseHandle)handle
- withBlock:(fbt_void_datasnapshot)block
- cancelCallback:(fbt_void_nserror)cancelBlock {
- // Note that we don't need to copy the callbacks here, FEventRegistration
- // callback properties set to copy
- FValueEventRegistration *registration =
- [[FValueEventRegistration alloc] initWithRepo:self.repo
- handle:handle
- callback:block
- cancelCallback:cancelBlock];
- dispatch_async([FIRDatabaseQuery sharedQueue], ^{
- [self.repo addEventRegistration:registration forQuery:self.querySpec];
- });
- }
- // Note: as with the above method, we may wish to expose this at some point.
- - (void)observeChildEventWithHandle:(FIRDatabaseHandle)handle
- withCallbacks:(NSDictionary *)callbacks
- cancelCallback:(fbt_void_nserror)cancelBlock {
- // Note that we don't need to copy the callbacks here, FEventRegistration
- // callback properties set to copy
- FChildEventRegistration *registration =
- [[FChildEventRegistration alloc] initWithRepo:self.repo
- handle:handle
- callbacks:callbacks
- cancelCallback:cancelBlock];
- dispatch_async([FIRDatabaseQuery sharedQueue], ^{
- [self.repo addEventRegistration:registration forQuery:self.querySpec];
- });
- }
- - (void)removeObserverWithHandle:(FIRDatabaseHandle)handle {
- FValueEventRegistration *event =
- [[FValueEventRegistration alloc] initWithRepo:self.repo
- handle:handle
- callback:nil
- cancelCallback:nil];
- dispatch_async([FIRDatabaseQuery sharedQueue], ^{
- [self.repo removeEventRegistration:event forQuery:self.querySpec];
- });
- }
- - (void)removeAllObservers {
- [self removeObserverWithHandle:NSNotFound];
- }
- - (void)keepSynced:(BOOL)keepSynced {
- if ([self.path.getFront isEqualToString:kDotInfoPrefix]) {
- [NSException raise:NSInvalidArgumentException
- format:@"Can't keep query on .info tree synced (this "
- @"already is the case)."];
- }
- dispatch_async([FIRDatabaseQuery sharedQueue], ^{
- [self.repo keepQuery:self.querySpec synced:keepSynced];
- });
- }
- - (void)observeSingleEventOfType:(FIRDataEventType)eventType
- withBlock:(fbt_void_datasnapshot)block {
- [self observeSingleEventOfType:eventType
- withBlock:block
- withCancelBlock:nil];
- }
- - (void)observeSingleEventOfType:(FIRDataEventType)eventType
- andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block {
- [self observeSingleEventOfType:eventType
- andPreviousSiblingKeyWithBlock:block
- withCancelBlock:nil];
- }
- - (void)observeSingleEventOfType:(FIRDataEventType)eventType
- withBlock:(fbt_void_datasnapshot)block
- withCancelBlock:(fbt_void_nserror)cancelBlock {
- // XXX: user reported memory leak in method
- // "When you copy a block, any references to other blocks from within that
- // block are copied if necessary—an entire tree may be copied (from the
- // top). If you have block variables and you reference a block from within
- // the block, that block will be copied."
- // http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html#//apple_ref/doc/uid/TP40007502-CH6-SW1
- // So... we don't need to do this since inside the on: we copy this block
- // off the stack to the heap.
- // __block fbt_void_datasnapshot userCallback = [callback copy];
- [self observeSingleEventOfType:eventType
- andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot,
- NSString *prevName) {
- if (block != nil) {
- block(snapshot);
- }
- }
- withCancelBlock:cancelBlock];
- }
- /**
- * Attaches a listener, waits for the first event, and then removes the listener
- */
- - (void)observeSingleEventOfType:(FIRDataEventType)eventType
- andPreviousSiblingKeyWithBlock:(fbt_void_datasnapshot_nsstring)block
- withCancelBlock:(fbt_void_nserror)cancelBlock {
- // XXX: user reported memory leak in method
- // "When you copy a block, any references to other blocks from within that
- // block are copied if necessary—an entire tree may be copied (from the
- // top). If you have block variables and you reference a block from within
- // the block, that block will be copied."
- // http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html#//apple_ref/doc/uid/TP40007502-CH6-SW1
- // So... we don't need to do this since inside the on: we copy this block
- // off the stack to the heap.
- // __block fbt_void_datasnapshot userCallback = [callback copy];
- __block FIRDatabaseHandle handle;
- __block BOOL firstCall = YES;
- fbt_void_datasnapshot_nsstring callback = [block copy];
- fbt_void_datasnapshot_nsstring wrappedCallback =
- ^(FIRDataSnapshot *snap, NSString *prevName) {
- if (firstCall) {
- firstCall = NO;
- [self removeObserverWithHandle:handle];
- callback(snap, prevName);
- }
- };
- fbt_void_nserror cancelCallback = [cancelBlock copy];
- handle = [self observeEventType:eventType
- andPreviousSiblingKeyWithBlock:wrappedCallback
- withCancelBlock:^(NSError *error) {
- [self removeObserverWithHandle:handle];
- if (cancelCallback) {
- cancelCallback(error);
- }
- }];
- }
- - (NSString *)description {
- return [NSString
- stringWithFormat:@"(%@ %@)", self.path, self.queryParams.description];
- }
- - (FIRDatabaseReference *)ref {
- return [[FIRDatabaseReference alloc] initWithRepo:self.repo path:self.path];
- }
- @end
|