blob: bd61cfa103fad6d786eb108abd1883187227fcfd [file] [log] [blame]
//
// SMTableViewDataSource.m
// StoreMad
//
// Created by Andrew Smith on 10/4/12.
// Copyright (c) 2012 Andrew B. Smith ( http://github.com/drewsmits ). All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "SMTableViewDataSource.h"
@interface SMTableViewDataSource ()
@property (nonatomic, weak) UITableView *tableView;
@end
@implementation SMTableViewDataSource
- (void)setupWithTableView:(UITableView *)tableView
fetchRequest:(NSFetchRequest *)fetchRequest
context:(NSManagedObjectContext *)context
sectionNameKeyPath:(NSString *)sectionNameKeyPath
cacheName:(NSString *)cacheName
{
self.tableView = tableView;
// Normal setup
[self setupWithFetchRequest:fetchRequest
context:context
sectionNameKeyPath:sectionNameKeyPath
cacheName:cacheName];
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
// Update sections
for (NSInteger i = self.tableView.numberOfSections; i < controller.sections.count; i++) {
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:i]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
// If you call reload here, you can run into a Core Data crash,
// where you see [__NSArrayM insertObject:atIndex:]: object cannot be nil.
// To be honest, I'm not sure what this is, could be a bug.
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
// Instead, you can just re-configure the cell at the index path, which
// does the same thing.
[self.dataSourceDelegate configureCell:[self.tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[self.tableView moveRowAtIndexPath:indexPath
toIndexPath:newIndexPath];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeMove:
break;
case NSFetchedResultsChangeUpdate:
break;
default:
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
if ([self.dataSourceDelegate respondsToSelector:@selector(fetchResultsDidChange)]) {
[self.dataSourceDelegate performSelector:@selector(fetchResultsDidChange)];
}
}
@end