File tree Expand file tree Collapse file tree 1 file changed +10
-5
lines changed Expand file tree Collapse file tree 1 file changed +10
-5
lines changed Original file line number Diff line number Diff line change @@ -114,13 +114,16 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
114
114
}
115
115
}
116
116
117
+ NSInteger lowerBound = 0 ;
118
+ NSInteger upperBound = self.snapshots .count ;
117
119
NSInteger index = self.count / 2 ;
118
- while (index >= 0 && index <= self.count ) {
120
+ while (index >= 0 && index <= upperBound) {
121
+
119
122
if (index == 0 ) {
120
- [self .snapshots insertObject: snapshot atIndex: index ];
123
+ [self .snapshots insertObject: snapshot atIndex: 0 ];
121
124
return 0 ;
122
125
}
123
- if (index == self.count ) {
126
+ if (index == self.snapshots . count ) {
124
127
[self .snapshots addObject: snapshot];
125
128
return index;
126
129
}
@@ -132,11 +135,13 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
132
135
133
136
if (left == NSOrderedDescending && right == NSOrderedAscending) {
134
137
// look left
135
- index /= 2 ;
138
+ upperBound = index;
139
+ index = (lowerBound + upperBound) / 2 ;
136
140
continue ;
137
141
} else if (left == NSOrderedAscending && right == NSOrderedDescending) {
138
142
// look right
139
- index = ((self.count - index) / 2 ) + index + 1 ;
143
+ lowerBound = index + 1 ;
144
+ index = (lowerBound + upperBound) / 2 ;
140
145
continue ;
141
146
} else if (left == NSOrderedDescending && right == NSOrderedDescending) {
142
147
// bad state (array is not sorted to begin with)
You can’t perform that action at this time.
0 commit comments