Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSFetchedResultsChangeUpdate not counted #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions TLIndexPathTools/Controllers/TLIndexPathController.m
Expand Up @@ -32,6 +32,7 @@
@interface TLIndexPathController ()
@property (strong, nonatomic) NSFetchedResultsController *backingController;
@property (strong, nonatomic) TLIndexPathDataModel *oldDataModel;
@property (strong, nonatomic) NSMutableArray *updatedObjects;
@property (nonatomic) BOOL performingBatchUpdate;
@property (nonatomic) BOOL pendingConvertFetchedObjectsToDataModel;
@end
Expand Down Expand Up @@ -165,8 +166,8 @@ - (void)setItems:(NSArray *)items
dataModel = [[TLIndexPathDataModel alloc] initWithItems:items];
} else {
dataModel = [[TLIndexPathDataModel alloc] initWithItems:items
sectionNameKeyPath:self.dataModel.sectionNameKeyPath
identifierKeyPath:self.dataModel.identifierKeyPath];
sectionNameKeyPath:self.dataModel.sectionNameKeyPath
identifierKeyPath:self.dataModel.identifierKeyPath];
}
self.dataModel = dataModel;
}
Expand Down Expand Up @@ -205,6 +206,13 @@ - (void)dequeuePendingUpdates
}
}
TLIndexPathUpdates *updates = [[TLIndexPathUpdates alloc] initWithOldDataModel:self.oldDataModel updatedDataModel:self.dataModel];

if (self.updatedObjects)
{
[updates addModifiedItems:self.updatedObjects];
self.updatedObjects = nil;
}

if ([self.delegate respondsToSelector:@selector(controller:didUpdateDataModel:)] && !self.ignoreDataModelChanges) {
[self.delegate controller:self didUpdateDataModel:updates];
}
Expand Down Expand Up @@ -288,6 +296,22 @@ - (TLIndexPathDataModel *)convertFetchedObjectsToDataModel {
}

#pragma mark - NSFetchedResultsControllerDelegate
- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller
{
self.updatedObjects = [NSMutableArray array];
}

- (void) controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (type == NSFetchedResultsChangeUpdate)
{
[self.updatedObjects addObject:anObject];
}
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
Expand Down
3 changes: 3 additions & 0 deletions TLIndexPathTools/Data Model/TLIndexPathUpdates.h
Expand Up @@ -45,4 +45,7 @@
- (void)performBatchUpdatesOnCollectionView:(UICollectionView *)collectionView;
- (void)performBatchUpdatesOnCollectionView:(UICollectionView *)collectionView completion:(void(^)(BOOL finished))completion;
- (id)initWithOldDataModel:(TLIndexPathDataModel *)oldDataModel updatedDataModel:(TLIndexPathDataModel *)updatedDataModel;

- (void) addModifiedItems:(NSArray *)items;

@end
58 changes: 42 additions & 16 deletions TLIndexPathTools/Data Model/TLIndexPathUpdates.m
Expand Up @@ -45,7 +45,7 @@ - (id)initWithOldDataModel:(TLIndexPathDataModel *)oldDataModel updatedDataModel
NSMutableArray *deletedItems = [[NSMutableArray alloc] init];
NSMutableArray *movedItems = [[NSMutableArray alloc] init];
NSMutableArray *modifiedItems = [[NSMutableArray alloc] init];

_insertedSectionNames = insertedSectionNames;
_deletedSectionNames = deletedSectionNames;
_movedSectionNames = movedSectionNames;
Expand All @@ -57,7 +57,7 @@ - (id)initWithOldDataModel:(TLIndexPathDataModel *)oldDataModel updatedDataModel
NSOrderedSet *oldSectionNames = [NSOrderedSet orderedSetWithArray:oldDataModel.sectionNames];
NSOrderedSet *updatedSectionNames = [NSOrderedSet orderedSetWithArray:updatedDataModel.sectionNames];

// Deleted and moved sections
// Deleted and moved sections
for (NSString *sectionName in oldSectionNames) {
if ([updatedSectionNames containsObject:sectionName]) {
NSInteger oldSection = [oldDataModel sectionForSectionName:sectionName];
Expand All @@ -79,26 +79,35 @@ - (id)initWithOldDataModel:(TLIndexPathDataModel *)oldDataModel updatedDataModel
}

// Deleted and moved items
NSOrderedSet *oldItems = [NSOrderedSet orderedSetWithArray:[oldDataModel items]];
NSOrderedSet *oldItems = [NSOrderedSet orderedSetWithArray:[oldDataModel items]];
for (id item in oldItems) {
NSIndexPath *oldIndexPath = [oldDataModel indexPathForItem:item];
NSString *sectionName = [oldDataModel sectionNameForSection:oldIndexPath.section];
if ([updatedDataModel containsItem:item]) {
NSIndexPath *updatedIndexPath = [updatedDataModel indexPathForItem:item];
//can't rely on isEqual, so must use compare
if ([oldIndexPath compare:updatedIndexPath] != NSOrderedSame) {
NSString *updatedSectionName = [updatedDataModel sectionNameForSection:updatedIndexPath.section];
//can't rely on isEqual, so must use compare
if ([oldIndexPath compare:updatedIndexPath] != NSOrderedSame || ![updatedSectionName isEqualToString:sectionName]) {
// Don't move items in moved sections
if (![movedSectionNames containsObject:sectionName]) {
// TODO Not sure if this is correct when moves are combined with inserts and/or deletes
// Don't report as moved if the only change is the section
// has moved
if (oldIndexPath.row == updatedIndexPath.row) {
NSString *oldSectionName = [oldDataModel sectionNameForSection:oldIndexPath.section];
NSString *updatedSectionName = [updatedDataModel sectionNameForSection:updatedIndexPath.section];
if ([oldSectionName isEqualToString:updatedSectionName]) continue;
}
[movedItems addObject:item];
}

if (![deletedSectionNames containsObject:sectionName])
[movedItems addObject:item];
else
{
[deletedItems addObject:item];

if (![insertedSectionNames containsObject:updatedSectionName])
[insertedItems addObject:item];
}
}
}
} else {
// Don't delete items in deleted sections
Expand Down Expand Up @@ -130,6 +139,21 @@ - (id)initWithOldDataModel:(TLIndexPathDataModel *)oldDataModel updatedDataModel
return self;
}

- (void) addModifiedItems:(NSArray *)items
{
NSMutableArray *updatedItems = [NSMutableArray array];
[updatedItems addObjectsFromArray:_modifiedItems];

for (id item in items)
{
if (![_modifiedItems containsObject:item] && ![_insertedItems containsObject:item] && ![_movedItems containsObject:item])
[updatedItems addObject:item];
}

_modifiedItems = updatedItems;
}


- (void)performBatchUpdatesOnTableView:(UITableView *)tableView withRowAnimation:(UITableViewRowAnimation)animation
{
[self performBatchUpdatesOnTableView:tableView withRowAnimation:animation completion:nil];
Expand All @@ -141,9 +165,9 @@ - (void)performBatchUpdatesOnTableView:(UITableView *)tableView withRowAnimation
[tableView reloadData];
return;
}

[CATransaction begin];

[CATransaction setCompletionBlock: ^{

//modified items have to be reloaded after all other batch updates
Expand All @@ -156,16 +180,18 @@ - (void)performBatchUpdatesOnTableView:(UITableView *)tableView withRowAnimation
for (id item in self.modifiedItems) {
NSIndexPath *indexPath = [self.updatedDataModel indexPathForItem:item];
[indexPaths addObject:indexPath];
[tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}
}

[tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];

}

if (completion) {
completion(YES);
}

}];

[tableView beginUpdates];

if (self.insertedSectionNames.count) {
Expand Down Expand Up @@ -262,9 +288,9 @@ - (void)performBatchUpdatesOnCollectionView:(UICollectionView *)collectionView c
}
return;
}

[collectionView performBatchUpdates:^{

if (self.insertedSectionNames.count) {
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
for (NSString *sectionName in self.insertedSectionNames) {
Expand Down Expand Up @@ -318,7 +344,7 @@ - (void)performBatchUpdatesOnCollectionView:(UICollectionView *)collectionView c
}

// TODO update modified items

} completion:^(BOOL finished) {
if (completion) {
completion(finished);
Expand Down