Skip to content

Commit 596afb0

Browse files
authored
Merge pull request #164 from morganchen12/joins
Support for joins
2 parents 6141ed6 + 8619311 commit 596afb0

20 files changed

+2014
-160
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:)]) {

FirebaseDatabaseUI/FirebaseArrayDelegate.h

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,43 @@
2222

2323
/**
2424
* A protocol to allow instances of FirebaseArray to raise events through a
25-
* delegate. Raises all
26-
* Firebase events except FIRDataEventTypeValue.
25+
* delegate. Raises all Firebase events except @c FIRDataEventTypeValue.
2726
*/
2827
@protocol FirebaseArrayDelegate<NSObject>
2928

3029
@optional
3130

3231
/**
3332
* Delegate method which is called whenever an object is added to a
34-
* FirebaseArray. On a
35-
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
36-
* [FIRDataEventTypeChildAdded](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
37-
* event being raised.
33+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
34+
* this corresponds to a @c FIRDataEventTypeChildAdded event being raised.
3835
* @param object The object added to the FirebaseArray
3936
* @param index The index the child was added at
4037
*/
4138
- (void)array:(FirebaseArray *)array didAddObject:(id)object atIndex:(NSUInteger)index;
4239

4340
/**
44-
* Delegate method which is called whenever an object is chinged in a
45-
* FirebaseArray. On a
46-
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
47-
* [FIRDataEventTypeChildChanged](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
48-
* event being raised.
41+
* Delegate method which is called whenever an object is changed in a
42+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
43+
* this corresponds to a @c FIRDataEventTypeChildChanged event being raised.
4944
* @param object The object that changed in the FirebaseArray
5045
* @param index The index the child was changed at
5146
*/
5247
- (void)array:(FirebaseArray *)array didChangeObject:(id)object atIndex:(NSUInteger)index;
5348

5449
/**
5550
* Delegate method which is called whenever an object is removed from a
56-
* FirebaseArray. On a
57-
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
58-
* [FIRDataEventTypeChildRemoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
59-
* event being raised.
51+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
52+
* this corresponds to a @c FIRDataEventTypeChildRemoved event being raised.
6053
* @param object The object removed from the FirebaseArray
6154
* @param index The index the child was removed at
6255
*/
6356
- (void)array:(FirebaseArray *)array didRemoveObject:(id)object atIndex:(NSUInteger)index;
6457

6558
/**
6659
* Delegate method which is called whenever an object is moved within a
67-
* FirebaseArray. On a
68-
* FirebaseArray synchronized to a Firebase reference, this corresponds to an
69-
* [FIRDataEventTypeChildMoved](https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types)
70-
* event being raised.
60+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
61+
* this corresponds to a @c FIRDataEventTypeChildMoved event being raised.
7162
* @param object The object that has moved locations in the FirebaseArray
7263
* @param fromIndex The index the child is being moved from
7364
* @param toIndex The index the child is being moved to

FirebaseDatabaseUI/FirebaseDatabaseUI.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ FOUNDATION_EXPORT const unsigned char FirebaseDatabaseUIVersionString[];
2727
#import <FirebaseDatabaseUI/FirebaseCollectionViewDataSource.h>
2828
#import <FirebaseDatabaseUI/FirebaseDataSource.h>
2929
#import <FirebaseDatabaseUI/FirebaseTableViewDataSource.h>
30-
31-
30+
#import <FirebaseDatabaseUI/FirebaseIndexArray.h>
31+
#import <FirebaseDatabaseUI/FirebaseIndexTableViewDataSource.h>
32+
#import <FirebaseDatabaseUI/FirebaseIndexCollectionViewDataSource.h>
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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 FirebaseIndexArray to raise events through a
29+
* delegate. Raises all Firebase events except @c FIRDataEventTypeValue.
30+
*/
31+
@protocol FirebaseIndexArrayDelegate <NSObject>
32+
33+
@optional
34+
35+
/**
36+
* Delegate method called when the database reference at an index has
37+
* finished loading its contents.
38+
* @param array The array containing the reference.
39+
* @param ref The reference that was loaded.
40+
* @param object The database reference's contents.
41+
* @param index The index of the reference that was loaded.
42+
*/
43+
- (void)array:(FirebaseIndexArray *)array
44+
reference:(FIRDatabaseReference *)ref
45+
didLoadObject:(FIRDataSnapshot *)object
46+
atIndex:(NSUInteger)index;
47+
48+
/**
49+
* Delegate method called when the database reference at an index has
50+
* failed to load contents.
51+
* @param array The array containing the reference.
52+
* @param ref The reference that failed to load.
53+
* @param index The index in the array of the reference that failed to load.
54+
* @param error The error that occurred.
55+
*/
56+
- (void)array:(FirebaseIndexArray *)array
57+
reference:(FIRDatabaseReference *)ref
58+
atIndex:(NSUInteger)index
59+
didFailLoadWithError:(NSError *)error;
60+
61+
/**
62+
* Delegate method which is called whenever an object is added to a
63+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
64+
* this corresponds to a @c FIRDataEventTypeChildAdded event being raised.
65+
* @param ref The database reference added to the array
66+
* @param index The index the reference was added at
67+
*/
68+
- (void)array:(FirebaseIndexArray *)array didAddReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
69+
70+
/**
71+
* Delegate method which is called whenever an object is changed in a
72+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
73+
* this corresponds to a @c FIRDataEventTypeChildChanged event being raised.
74+
* @param ref The database reference that changed in the array
75+
* @param index The index the reference was changed at
76+
*/
77+
- (void)array:(FirebaseIndexArray *)array didChangeReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
78+
79+
/**
80+
* Delegate method which is called whenever an object is removed from a
81+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
82+
* this corresponds to a @c FIRDataEventTypeChildRemoved event being raised.
83+
* @param ref The database reference removed from the array
84+
* @param index The index the reference was removed at
85+
*/
86+
- (void)array:(FirebaseIndexArray *)array didRemoveReference:(FIRDatabaseReference *)ref atIndex:(NSUInteger)index;
87+
88+
/**
89+
* Delegate method which is called whenever an object is moved within a
90+
* FirebaseArray. On a FirebaseArray synchronized to a Firebase reference,
91+
* this corresponds to a @c FIRDataEventTypeChildMoved event being raised.
92+
* @param ref The database reference that has moved locations
93+
* @param fromIndex The index the reference is being moved from
94+
* @param toIndex The index the reference is being moved to
95+
*/
96+
- (void)array:(FirebaseIndexArray *)array didMoveReference:(FIRDatabaseReference *)ref fromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;
97+
98+
/**
99+
* Delegate method which is called whenever the backing query is canceled. This error is fatal
100+
* and the index array will become unusable afterward, so please handle it appropriately
101+
* (i.e. by displaying a modal error explaining why there's no content).
102+
* @param error the error that was raised
103+
*/
104+
- (void)array:(FirebaseIndexArray *)array queryCancelledWithError:(NSError *)error;
105+
106+
@end
107+
108+
/**
109+
* A FirebaseIndexArray instance uses a query's contents to query children of
110+
* a separate database reference, which is useful for displaying an indexed list
111+
* of data as described in https://firebase.google.com/docs/database/ios/structure-data
112+
*/
113+
@interface FirebaseIndexArray : NSObject
114+
115+
/**
116+
* An immutable copy of the loaded contents in the array. Returns an
117+
* empty array if no contents have loaded yet.
118+
*/
119+
@property(nonatomic, copy, readonly) NSArray<FIRDataSnapshot *> *items;
120+
121+
/**
122+
* The delegate that this array should forward events to.
123+
*/
124+
@property(nonatomic, weak) id<FirebaseIndexArrayDelegate> delegate;
125+
126+
/**
127+
* Returns the number of items in the array.
128+
*/
129+
@property(nonatomic, readonly) NSUInteger count;
130+
131+
- (instancetype)init NS_UNAVAILABLE;
132+
133+
/**
134+
* Initializes a FirebaseIndexArray with an index query and a data query.
135+
* The array expects the keys of the children of the index query to match exactly children
136+
* of the data query.
137+
* @param index A Firebase database query whose childrens' keys are all children
138+
* of the data query.
139+
* @param data A Firebase database reference whose children will be fetched and used
140+
* to populate the array's contents according to the index query.
141+
* @param delegate The delegate that events should be forwarded to.
142+
*/
143+
- (instancetype)initWithIndex:(id<FIRDataObservable>)index
144+
data:(id<FIRDataObservable>)data
145+
delegate:(nullable id<FirebaseIndexArrayDelegate>)delegate NS_DESIGNATED_INITIALIZER;
146+
147+
/**
148+
* Initializes a FirebaseIndexArray with an index query and a data query.
149+
* The array expects the keys of the children of the index query to be children
150+
* of the data query.
151+
* @param index A Firebase database query whose childrens' keys are all children
152+
* of the data query.
153+
* @param data A Firebase database reference whose children will be fetched and used
154+
* to populate the array's contents according to the index query.
155+
*/
156+
- (instancetype)initWithIndex:(id<FIRDataObservable>)index
157+
data:(id<FIRDataObservable>)data;
158+
159+
/**
160+
* Returns the snapshot at the given index, if it has loaded.
161+
* Raises a fatal error if the index is out of bounds.
162+
* @param index The index of the requested snapshot.
163+
* @return A snapshot, or nil if one has not yet been loaded.
164+
*/
165+
- (nullable FIRDataSnapshot *)objectAtIndex:(NSUInteger)index;
166+
167+
/**
168+
* Removes all observers from all queries managed by this array and renders this array
169+
* unusable.
170+
*/
171+
- (void)invalidate;
172+
173+
@end
174+
175+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)