Skip to content

Commit

Permalink
Fix issue where grouping objects by section in RKTableController and …
Browse files Browse the repository at this point in the history
…loading an empty collection would fail to refresh table view.
  • Loading branch information
blakewatters committed May 25, 2012
1 parent fa88dd4 commit 8647fa5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/UI/RKTableController.m
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ - (void)objectLoader:(RKObjectLoader *)loader didLoadObjects:(NSArray *)objects

if (self.sectionNameKeyPath) {
NSArray *sectionedObjects = [objects sectionsGroupedByKeyPath:self.sectionNameKeyPath];
if ([sectionedObjects count] == 0) {
[self removeAllSections];
}
for (NSArray *sectionOfObjects in sectionedObjects) {
NSUInteger sectionIndex = [sectionedObjects indexOfObject:sectionOfObjects];
if (sectionIndex >= [self sectionCount]) {
Expand Down
37 changes: 37 additions & 0 deletions Tests/Application/UI/RKTableControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,43 @@ - (void)testLoadCollectionOfObjectsAndMapThemIntoSections {
assertThatInt(tableController.rowCount, is(equalToInt(3)));
}

- (void)testLoadingACollectionOfObjectsIntoSectionsAndThenLoadingAnEmptyCollectionChangesTableToEmpty {
RKObjectManager* objectManager = [RKTestFactory objectManager];
objectManager.client.cachePolicy = RKRequestCachePolicyNone;
RKTableControllerTestTableViewController* viewController = [RKTableControllerTestTableViewController new];
RKTableController* tableController = [RKTableController tableControllerForTableViewController:viewController];
tableController.objectManager = objectManager;
[tableController mapObjectsWithClass:[RKTestUser class] toTableCellsWithMapping:[RKTableViewCellMapping cellMappingUsingBlock:^(RKTableViewCellMapping* mapping) {
mapping.cellClass = [RKTestUserTableViewCell class];
[mapping mapKeyPath:@"name" toAttribute:@"textLabel.text"];
[mapping mapKeyPath:@"nickName" toAttribute:@"detailTextLabel.text"];
}]];
tableController.sectionNameKeyPath = @"name";
RKTableControllerTestDelegate* delegate = [RKTableControllerTestDelegate tableControllerDelegate];
delegate.timeout = 10;
tableController.delegate = delegate;
[tableController loadTableFromResourcePath:@"/JSON/users.json" usingBlock:^(RKObjectLoader* objectLoader) {
objectLoader.objectMapping = [RKObjectMapping mappingForClass:[RKTestUser class] usingBlock:^(RKObjectMapping* mapping) {
[mapping mapAttributes:@"name", nil];
}];
}];
[delegate waitForLoad];
assertThatBool([tableController isLoaded], is(equalToBool(YES)));
assertThatInt(tableController.sectionCount, is(equalToInt(3)));
assertThatInt(tableController.rowCount, is(equalToInt(3)));
delegate = [RKTableControllerTestDelegate tableControllerDelegate];
delegate.timeout = 10;
tableController.delegate = delegate;
[tableController loadTableFromResourcePath:@"/204" usingBlock:^(RKObjectLoader* objectLoader) {
objectLoader.objectMapping = [RKObjectMapping mappingForClass:[RKTestUser class] usingBlock:^(RKObjectMapping* mapping) {
[mapping mapAttributes:@"name", nil];
}];
}];
[delegate waitForLoad];
assertThatBool([tableController isLoaded], is(equalToBool(YES)));
assertThatBool([tableController isEmpty], is(equalToBool(YES)));
}

#pragma mark - RKTableViewDelegate Tests

- (void)testNotifyTheDelegateWhenLoadingStarts {
Expand Down
6 changes: 6 additions & 0 deletions Tests/Server/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def render_fixture(path, options = {})
content_type 'application/json'
"".to_json
end

get '/204' do
status 204
content_type 'application/json'
"".to_json
end

get '/403' do
status 403
Expand Down

0 comments on commit 8647fa5

Please sign in to comment.