From e8e4022dc10a4b0d89f4eb23be910d4bda551b70 Mon Sep 17 00:00:00 2001 From: Atsushi Nagase Date: Thu, 2 Jun 2016 17:41:28 +0900 Subject: [PATCH] Fix assersion failure when moving objects between sections --- .../Source/RBQFetchedResultsController.m | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/RBQFetchedResultsController/Source/RBQFetchedResultsController.m b/RBQFetchedResultsController/Source/RBQFetchedResultsController.m index f1a6894..cdb8aec 100644 --- a/RBQFetchedResultsController/Source/RBQFetchedResultsController.m +++ b/RBQFetchedResultsController/Source/RBQFetchedResultsController.m @@ -655,7 +655,7 @@ - (NSSet *)safeObjectsFromChanges:(NSArray *)changes safeObject = [RBQSafeRealmObject safeObjectFromObject:object]; } else { - NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstObjectIndex <= %@ AND lastObjectIndex >= %@", index, index]; + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstObjectIndex <= %@ AND lastObjectIndex >= %@ AND objects.@count > 0", index, index]; RLMResults *sections = [RBQSectionCacheObject objectsInRealm:cacheRealm withPredicate:predicate]; RBQSectionCacheObject *section = sections.firstObject; NSUInteger row = index.unsignedIntegerValue - section.firstObjectIndex; @@ -891,7 +891,7 @@ - (void)applyDerivedChangesToCache:(RBQDerivedChangesObject *)derivedChanges for (NSOrderedSet *objectChanges in @[derivedChanges.deletedObjectChanges, derivedChanges.insertedObjectChanges, derivedChanges.movedObjectChanges]) { - + for (RBQObjectChangeObject *objectChange in objectChanges) { if (objectChange.changeType == NSFetchedResultsChangeDelete) { @@ -915,12 +915,12 @@ - (void)applyDerivedChangesToCache:(RBQDerivedChangesObject *)derivedChanges RBQSectionCacheObject *section = [RBQSectionCacheObject objectInRealm:state.cacheRealm forPrimaryKey:objectChange.updatedCacheObject.sectionKeyPathValue]; - -#ifdef DEBUG - NSAssert(objectChange.updatedIndexpath.row <= section.objects.count, @"Attemting to insert at index beyond bounds!"); -#endif + NSInteger insertIndex = objectChange.updatedIndexpath.row; + if (insertIndex >= section.objects.count) { + insertIndex = section.objects.count - 1; + } [section.objects insertObject:objectChange.updatedCacheObject - atIndex:objectChange.updatedIndexpath.row]; + atIndex:insertIndex]; objectChange.updatedCacheObject.section = section; }