@@ -187,7 +187,18 @@ - (NSUInteger)indexForKey:(NSString *)key {
187187- (void )insertSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
188188 NSUInteger index = 0 ;
189189 if (previous != nil ) {
190- index = [self indexForKey: previous] + 1 ;
190+ NSInteger previousChildIndex = (NSInteger )[self indexForKey: previous];
191+
192+ if (previousChildIndex == NSNotFound ) {
193+ NSString *reason = [NSString stringWithFormat: @" Attempted to insert snapshot with unknown"
194+ @" previousChildKey %@ into array: %@ " , previous, self .snapshots];
195+ NSException *exception = [NSException exceptionWithName: NSInternalInconsistencyException
196+ reason: reason
197+ userInfo: nil ];
198+ @throw exception;
199+ }
200+
201+ index = previousChildIndex + 1 ;
191202 }
192203
193204 [self .snapshots insertObject: snap atIndex: index];
@@ -200,6 +211,15 @@ - (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
200211- (void )removeSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
201212 NSUInteger index = [self indexForKey: snap.key];
202213
214+ if (index == NSNotFound ) {
215+ NSString *reason = [NSString stringWithFormat: @" Attempted to remove snapshot with unknown"
216+ @" key %@ from array: %@ " , snap.key, self .snapshots];
217+ NSException *exception = [NSException exceptionWithName: NSInternalInconsistencyException
218+ reason: reason
219+ userInfo: nil ];
220+ @throw exception;
221+ }
222+
203223 [self .snapshots removeObjectAtIndex: index];
204224
205225 if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
@@ -210,6 +230,15 @@ - (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
210230- (void )changeSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
211231 NSUInteger index = [self indexForKey: snap.key];
212232
233+ if (index == NSNotFound ) {
234+ NSString *reason = [NSString stringWithFormat: @" Attempted to replace snapshot with unknown"
235+ @" key %@ in array: %@ " , snap.key, self .snapshots];
236+ NSException *exception = [NSException exceptionWithName: NSInternalInconsistencyException
237+ reason: reason
238+ userInfo: nil ];
239+ @throw exception;
240+ }
241+
213242 [self .snapshots replaceObjectAtIndex: index withObject: snap];
214243
215244 if ([self .delegate respondsToSelector: @selector (array:didChangeObject:atIndex: )]) {
@@ -219,6 +248,16 @@ - (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
219248
220249- (void )moveSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
221250 NSUInteger fromIndex = [self indexForKey: snap.key];
251+
252+ if (fromIndex == NSNotFound ) {
253+ NSString *reason = [NSString stringWithFormat: @" Attempted to remove snapshot with unknown"
254+ @" key %@ from array: %@ " , snap.key, self .snapshots];
255+ NSException *exception = [NSException exceptionWithName: NSInternalInconsistencyException
256+ reason: reason
257+ userInfo: nil ];
258+ @throw exception;
259+ }
260+
222261 [self .snapshots removeObjectAtIndex: fromIndex];
223262
224263 NSUInteger toIndex = 0 ;
0 commit comments