Skip to content

Commit

Permalink
Generic NSFetchedResultsController data source
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Apr 18, 2014
1 parent 63674a7 commit 31ff37b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
8 changes: 8 additions & 0 deletions TGRDataSource.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
990C568719017CB400E382F1 /* TGRDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 990C568619017CB400E382F1 /* TGRDataSource.m */; };
990C568A19017DAD00E382F1 /* NSObject+Abstract.m in Sources */ = {isa = PBXBuildFile; fileRef = 990C568919017DAD00E382F1 /* NSObject+Abstract.m */; };
990C568D19017EBA00E382F1 /* TGRArrayDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 990C568C19017EBA00E382F1 /* TGRArrayDataSource.m */; };
993DB7E71901990000653166 /* TGRFetchedResultsDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 993DB7E61901990000653166 /* TGRFetchedResultsDataSource.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -57,6 +58,8 @@
990C568919017DAD00E382F1 /* NSObject+Abstract.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+Abstract.m"; sourceTree = "<group>"; };
990C568B19017EBA00E382F1 /* TGRArrayDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TGRArrayDataSource.h; sourceTree = "<group>"; };
990C568C19017EBA00E382F1 /* TGRArrayDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TGRArrayDataSource.m; sourceTree = "<group>"; };
993DB7E51901990000653166 /* TGRFetchedResultsDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TGRFetchedResultsDataSource.h; sourceTree = "<group>"; };
993DB7E61901990000653166 /* TGRFetchedResultsDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TGRFetchedResultsDataSource.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -121,6 +124,8 @@
990C568619017CB400E382F1 /* TGRDataSource.m */,
990C568B19017EBA00E382F1 /* TGRArrayDataSource.h */,
990C568C19017EBA00E382F1 /* TGRArrayDataSource.m */,
993DB7E51901990000653166 /* TGRFetchedResultsDataSource.h */,
993DB7E61901990000653166 /* TGRFetchedResultsDataSource.m */,
);
path = TGRDataSource;
sourceTree = "<group>";
Expand Down Expand Up @@ -234,6 +239,7 @@
buildActionMask = 2147483647;
files = (
990C568719017CB400E382F1 /* TGRDataSource.m in Sources */,
993DB7E71901990000653166 /* TGRFetchedResultsDataSource.m in Sources */,
990C568A19017DAD00E382F1 /* NSObject+Abstract.m in Sources */,
990C568D19017EBA00E382F1 /* TGRArrayDataSource.m in Sources */,
);
Expand Down Expand Up @@ -416,6 +422,7 @@
990C568119017A3400E382F1 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
990C568219017A3400E382F1 /* Build configuration list for PBXNativeTarget "TGRDataSourceTests" */ = {
isa = XCConfigurationList;
Expand All @@ -424,6 +431,7 @@
990C568419017A3400E382F1 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
50 changes: 50 additions & 0 deletions TGRDataSource/TGRFetchedResultsDataSource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// TGRFetchedResultsDataSource.h
//
// Copyright (c) 2014 Guillermo Gonzalez
//
// 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 <CoreData/CoreData.h>

#import "TGRDataSource.h"

/**
A `TGRDataSource` subclass that takes data from a `NSFetchedResultsController`.
*/
@interface TGRFetchedResultsDataSource : TGRDataSource

/**
The `NSFetchedResultsController` object managed by this data source.
*/
@property (strong, nonatomic, readonly) NSFetchedResultsController *fetchedResultsController;

/**
Initializes the data source.
@param controller The `NSFetchedResultsController` object managed by this data source.
@param reuseIdentifier The cell reuse identifier.
@param configureCellBlock A block that will be called when the view asks for a cell in a particular location.
@return An initialized data source.
*/
- (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller
cellReuseIdentifier:(NSString *)reuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock;

@end
72 changes: 72 additions & 0 deletions TGRDataSource/TGRFetchedResultsDataSource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// TGRFetchedResultsDataSource.m
//
// Copyright (c) 2014 Guillermo Gonzalez
//
// 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 "TGRFetchedResultsDataSource.h"

@implementation TGRFetchedResultsDataSource

- (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller
cellReuseIdentifier:(NSString *)reuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock
{
self = [super initWithCellReuseIdentifier:reuseIdentifier configureCellBlock:configureCellBlock];

if (self) {
_fetchedResultsController = controller;
}

return self;
}

- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
return [self.fetchedResultsController objectAtIndexPath:indexPath];
}

- (NSIndexPath *)indexPathForItem:(id)item {
return [self.fetchedResultsController indexPathForObject:item];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.fetchedResultsController.sections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id<NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResultsController.sections[section];
return [sectionInfo numberOfObjects];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [self.fetchedResultsController.sections count];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResultsController.sections[section];
return [sectionInfo numberOfObjects];
}

@end

0 comments on commit 31ff37b

Please sign in to comment.