Skip to content

Commit 868e929

Browse files
committed
support for joins
1 parent 65c5ca3 commit 868e929

File tree

10 files changed

+865
-8
lines changed

10 files changed

+865
-8
lines changed

FirebaseDatabaseUI/FirebaseArray.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
NS_ASSUME_NONNULL_BEGIN
2626

27-
@protocol FIRDataObservable
27+
@protocol FIRDataObservable <NSObject>
2828
@required
2929

3030
- (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType
@@ -33,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN
3333

3434
- (void)removeObserverWithHandle:(FIRDatabaseHandle)handle;
3535

36+
- (id<FIRDataObservable>)child:(NSString *)path;
37+
3638
@end
3739

3840
@interface FIRDatabaseQuery (FIRDataObservable) <FIRDataObservable>
@@ -71,10 +73,25 @@ NS_ASSUME_NONNULL_BEGIN
7173
/**
7274
* Initalizes FirebaseArray with a Firebase query (FIRDatabaseQuery) or database reference
7375
* (FIRDatabaseReference).
74-
* @param query A query or Firebase database reference
76+
* @param query A query or Firebase database reference
77+
* @param delegate An object conforming to FirebaseArrayDelegate that should receive delegate messages.
78+
* @return A FirebaseArray instance
79+
*/
80+
- (instancetype)initWithQuery:(id<FIRDataObservable>)query
81+
delegate:(nullable id<FirebaseArrayDelegate>)delegate NS_DESIGNATED_INITIALIZER;
82+
83+
/**
84+
* Initalizes FirebaseArray with a Firebase query (FIRDatabaseQuery) or database reference
85+
* (FIRDatabaseReference).
86+
* @param query A query or Firebase database reference
7587
* @return A FirebaseArray instance
7688
*/
77-
- (instancetype)initWithQuery:(id<FIRDataObservable>)query NS_DESIGNATED_INITIALIZER;
89+
- (instancetype)initWithQuery:(id<FIRDataObservable>)query;
90+
91+
/**
92+
* See `initWithQuery:`
93+
*/
94+
+ (instancetype)arrayWithQuery:(id<FIRDataObservable>)query;
7895

7996
- (instancetype)init NS_UNAVAILABLE;
8097

FirebaseDatabaseUI/FirebaseArray.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,27 @@ @implementation FirebaseArray
3939

4040
#pragma mark - Initializer methods
4141

42-
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query {
42+
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query delegate:(id<FirebaseArrayDelegate>)delegate {
4343
NSParameterAssert(query != nil);
4444
self = [super init];
4545
if (self) {
4646
self.snapshots = [NSMutableArray array];
4747
self.query = query;
4848
self.handles = [NSMutableSet setWithCapacity:4];
49-
49+
self.delegate = delegate;
5050
[self initListeners];
5151
}
5252
return self;
5353
}
5454

55+
- (instancetype)initWithQuery:(id<FIRDataObservable>)query {
56+
return [self initWithQuery:query delegate:nil];
57+
}
58+
59+
+ (instancetype)arrayWithQuery:(id<FIRDataObservable>)query {
60+
return [[self alloc] initWithQuery:query];
61+
}
62+
5563
#pragma mark - Memory management methods
5664

5765
- (void)dealloc {
@@ -130,6 +138,7 @@ - (void)initListeners {
130138
toIndex = prevIndex + 1;
131139
}
132140
}
141+
133142
[self.snapshots insertObject:snapshot atIndex:toIndex];
134143

135144
if ([self.delegate respondsToSelector:@selector(array:didMoveObject:fromIndex:toIndex:)]) {
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// clang-format off
2+
3+
//
4+
// Copyright (c) 2016 Google Inc.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
// clang-format on
20+
21+
#import "FirebaseArray.h"
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
@class FirebaseIndexArray;
26+
27+
/**
28+
* A protocol to allow instances of FirebaseArray to raise events through a
29+
* delegate. Raises all
30+
* Firebase events except FIRDataEventTypeValue.
31+
*/
32+
@protocol FirebaseIndexArrayDelegate<NSObject>
33+
34+
@optional
35+
36+
/**
37+
* Delegate method called when the database reference at an index has
38+
* finished loading its contents.
39+
* @param array The array calling this method.
40+
* @param reference The reference that was loaded.
41+
* @param object The database reference's contents.
42+
* @param index The index of the reference that was loaded.
43+
*/
44+
- (void)array:(FirebaseIndexArray *)array
45+
reference:(FIRDatabaseReference *)ref
46+
didLoadObject:(FIRDataSnapshot *)object
47+
atIndex:(NSUInteger)index;
48+
49+
/**
50+
* Delegate method which is called whenever an object is added to a
51+
* FirebaseArray. On a
52+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
53+
* [FIRDataEventTypeChildAdded](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
54+
* event being raised.
55+
* @param ref The database reference added to the array
56+
* @param index The index the child was added at
57+
*/
58+
- (void)array:(FirebaseIndexArray *)array didAddReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
59+
60+
/**
61+
* Delegate method which is called whenever an object is chinged in a
62+
* FirebaseArray. On a
63+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
64+
* [FIRDataEventTypeChildChanged](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
65+
* event being raised.
66+
* @param object The database reference that changed in the array
67+
* @param index The index the child was changed at
68+
*/
69+
- (void)array:(FirebaseIndexArray *)array didChangeReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
70+
71+
/**
72+
* Delegate method which is called whenever an object is removed from a
73+
* FirebaseArray. On a
74+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
75+
* [FIRDataEventTypeChildRemoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
76+
* event being raised.
77+
* @param object The database reference removed from the array
78+
* @param index The index the child was removed at
79+
*/
80+
- (void)array:(FirebaseIndexArray *)array didRemoveReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
81+
82+
/**
83+
* Delegate method which is called whenever an object is moved within a
84+
* FirebaseArray. On a
85+
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
86+
* [FIRDataEventTypeChildMoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
87+
* event being raised.
88+
* @param object The database reference that has moved locations
89+
* @param fromIndex The index the child is being moved from
90+
* @param toIndex The index the child is being moved to
91+
*/
92+
- (void)array:(FirebaseIndexArray *)array didMoveReference:(FIRDatabaseReference *)ref fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;
93+
94+
/**
95+
* Delegate method which is called whenever the backing query is canceled. This error is fatal
96+
* and the index array will become unusable afterward, so please handle it appropriately.
97+
* @param error the error that was raised
98+
*/
99+
- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error;
100+
101+
@end
102+
103+
@interface FirebaseIndexArray : NSObject
104+
105+
@property(nonatomic, copy, readonly) NSArray<FIRDataSnapshot *> *items;
106+
107+
@property(nonatomic, weak) id<FirebaseIndexArrayDelegate> delegate;
108+
109+
- (instancetype)init NS_UNAVAILABLE;
110+
111+
/**
112+
* Initializes a FirebaseIndexArray with an index query and a data query.
113+
* The array expects the keys of the children of the index query to be children
114+
* of the data query.
115+
* @param index A Firebase database query whose childrens' keys are all children
116+
* of the data query.
117+
* @param data A Firebase database reference whose children will be fetched and used
118+
* to populate the array's contents according to the index query.
119+
* @param delegate The delegate that events should be forwarded to.
120+
*/
121+
- (instancetype)initWithIndex:(id<FIRDataObservable>)index
122+
data:(id<FIRDataObservable>)data
123+
delegate:(nullable id<FirebaseIndexArrayDelegate>)delegate NS_DESIGNATED_INITIALIZER;
124+
125+
/**
126+
* Initializes a FirebaseIndexArray with an index query and a data query.
127+
* The array expects the keys of the children of the index query to be children
128+
* of the data query.
129+
* @param index A Firebase database query whose childrens' keys are all children
130+
* of the data query.
131+
* @param data A Firebase database reference whose children will be fetched and used
132+
* to populate the array's contents according to the index query.
133+
*/
134+
- (instancetype)initWithIndex:(id<FIRDataObservable>)index
135+
data:(id<FIRDataObservable>)data;
136+
137+
/**
138+
* Removes all observers from all queries managed by this array and renders this array
139+
* unusable.
140+
*/
141+
- (void)invalidate;
142+
143+
@end
144+
145+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)