Skip to content

Commit ac9c308

Browse files
committed
cheat at subclassing to expose less public mutability
1 parent 08a1bff commit ac9c308

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

FirebaseDatabaseUI/FUIArray.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,40 +118,36 @@ NS_ASSUME_NONNULL_BEGIN
118118

119119
/**
120120
* Called when the Firebase query sends a FIRDataEventTypeChildAdded event. Override this
121-
* to provide custom insertion logic.
121+
* to provide custom insertion logic. Don't call this method directly.
122122
* @param snap The snapshot that was inserted.
123123
* @param previous The key of the sibling preceding the inserted snapshot.
124124
*/
125125
- (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
126126

127127
/**
128128
* Called when the Firebase query sends a FIRDataEventTypeChildRemoved event. Override this
129-
* to provide custom removal logic.
129+
* to provide custom removal logic. Don't call this method directly.
130130
* @param snap The snapshot that was removed.
131131
* @param previous The key of the sibling preceding the removed snapshot.
132132
*/
133133
- (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
134134

135135
/**
136136
* Called when the Firebase query sends a FIRDataEventTypeChildChanged event. Override this
137-
* to provide custom on change logic.
137+
* to provide custom on change logic. Don't call this method directly.
138138
* @param snap The snapshot whose value was changed.
139139
* @param previous The key of the sibling preceding the changed snapshot.
140140
*/
141141
- (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
142142

143143
/**
144144
* Called when the Firebase query sends a FIRDataEventTypeChildMoved event. Override this
145-
* to provide custom move logic.
145+
* to provide custom move logic. Don't call this method directly.
146146
* @param snap The snapshot that was moved.
147147
* @param previous The key of the sibling preceding the moved snapshot at its new location.
148148
*/
149149
- (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(nullable NSString *)previous;
150150

151-
- (void)removeSnapshotAtIndex:(NSUInteger)index;
152-
- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index;
153-
- (void)addSnapshot:(FIRDataSnapshot *)snap;
154-
155151
@end
156152

157153
NS_ASSUME_NONNULL_END

FirebaseDatabaseUI/FUISortedArray.m

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ @interface FUISortedArray ()
2323
*/
2424
@property (nonatomic, copy, nonnull) NSComparisonResult (^sortDescriptor)(FIRDataSnapshot *, FIRDataSnapshot *);
2525

26+
/**
27+
* The backing collection that holds all of the array's data.
28+
*/
29+
@property (strong, nonatomic) NSMutableArray<FIRDataSnapshot *> *snapshots;
30+
2631
/**
2732
* A set containing the query observer handles that should be released when
2833
* this array is freed.
@@ -32,6 +37,9 @@ @interface FUISortedArray ()
3237
@end
3338

3439
@implementation FUISortedArray
40+
// Cheating at subclassing, but this @dynamic avoids
41+
// duplicating storage without exposing mutability publicly
42+
@dynamic snapshots;
3543

3644
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
3745
delegate:(id<FUICollectionDelegate>)delegate
@@ -57,7 +65,7 @@ - (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
5765
NSInteger index = [self indexForKey:snap.key];
5866
if (index == NSNotFound) { /* error */ return; }
5967

60-
[self removeSnapshotAtIndex:index];
68+
[self.snapshots removeObjectAtIndex:index];
6169
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
6270
[self.delegate array:self didRemoveObject:snap atIndex:index];
6371
}
@@ -71,7 +79,7 @@ - (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
7179

7280
// Since changes can change ordering, model changes as a deletion and an insertion.
7381
FIRDataSnapshot *removed = [self snapshotAtIndex:index];
74-
[self removeSnapshotAtIndex:index];
82+
[self.snapshots removeObjectAtIndex:index];
7583
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
7684
[self.delegate array:self didRemoveObject:removed atIndex:index];
7785
}
@@ -92,29 +100,29 @@ - (NSArray *)items {
92100

93101
- (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
94102
if (self.count == 0) {
95-
[self addSnapshot:snapshot];
103+
[self.snapshots addObject:snapshot];
96104
return 0;
97105
}
98106
if (self.count == 1) {
99107
NSComparisonResult result = self.sortDescriptor(snapshot, [self snapshotAtIndex:0]);
100108
switch (result) {
101109
case NSOrderedDescending:
102-
[self addSnapshot:snapshot];
110+
[self.snapshots addObject:snapshot];
103111
return 1;
104112
default:
105-
[self insertSnapshot:snapshot atIndex:0];
113+
[self.snapshots insertObject:snapshot atIndex:0];
106114
return 0;
107115
}
108116
}
109117

110118
NSInteger index = self.count / 2;
111119
while (index >= 0 && index <= self.count) {
112120
if (index == 0) {
113-
[self insertSnapshot:snapshot atIndex:index];
121+
[self.snapshots insertObject:snapshot atIndex:index];
114122
return 0;
115123
}
116124
if (index == self.count) {
117-
[self addSnapshot:snapshot];
125+
[self.snapshots addObject:snapshot];
118126
return index;
119127
}
120128

@@ -136,7 +144,7 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
136144
NSAssert(NO, @"FUISortedArray %@'s sort descriptor returned inconsistent results!", self);
137145
} else {
138146
// good
139-
[self insertSnapshot:snapshot atIndex:index];
147+
[self.snapshots insertObject:snapshot atIndex:index];
140148
return index;
141149
}
142150
}

0 commit comments

Comments
 (0)