Skip to content
This repository has been archived by the owner on Jan 19, 2018. It is now read-only.

Commit

Permalink
Fix a crash and other bad stuff when you tap a cell during a load
Browse files Browse the repository at this point in the history
I was very stupid when I wrote this code.
  • Loading branch information
cdzombak committed Oct 31, 2013
1 parent 12df5a8 commit 4081e37
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions CAENLabStatus/DZCLabsListViewController.m
Expand Up @@ -30,8 +30,8 @@ typedef NS_ENUM(NSUInteger, DZCLabsListFilter) {
@interface DZCLabsListViewController ()

@property (nonatomic, strong) NSMutableArray *labOrdering;
@property (nonatomic, strong) NSMutableDictionary *labsByStatus;
@property (nonatomic, strong) NSMutableArray *statusForTableViewSection;
@property (nonatomic, strong) NSDictionary *labsByStatus;
@property (nonatomic, strong) NSArray *statusForTableViewSection;

@property (nonatomic, readonly) UISegmentedControl *filterControl;
@property (nonatomic, assign) DZCLabsListFilter selectedFilter;
Expand Down Expand Up @@ -113,7 +113,6 @@ - (void)refreshData:(UIRefreshControl *)sender {

- (void)refreshData
{
self.labsByStatus = nil; // TODO I think this causes some interaction which can cause a crash sometimes
[self.dataController clearCache];
[self loadData];
}
Expand All @@ -123,8 +122,6 @@ - (void)loadData
[self.dataController labsAndStatusesWithBlock:^(NSDictionary *labsResult, NSError *error) {

[self.refreshControl endRefreshing];

self.labsByStatus = nil;

if (error) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error Retrieving Data", nil)
Expand All @@ -134,51 +131,54 @@ - (void)loadData
otherButtonTitles:nil]
show];

self.labsByStatus = nil;
self.labsByStatus = @{};
self.statusForTableViewSection = @[];
[self.tableView reloadData];

return;
}

// labsResult is a dict mapping DZCLab => (NSNumber)status
NSMutableDictionary *filteredLabs = [NSMutableDictionary dictionary];
[labsResult enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSMutableDictionary *filteredLabStatusesByLab = [NSMutableDictionary dictionary];
[labsResult enumerateKeysAndObjectsUsingBlock:^(DZCLab *lab, NSNumber *status, BOOL *stop) {
BOOL includeThisLab = (self.selectedFilter == DZCLabsListFilterAll);

DZCLab *lab = key;
// 42.285 // todo remove hardcoded values
BOOL isNorth = ([lab.latitude doubleValue] > 42.285);
if (self.selectedFilter == DZCLabsListFilterNorth && isNorth) includeThisLab = YES;
if (self.selectedFilter == DZCLabsListFilterCentral && !isNorth) includeThisLab = YES;

if (includeThisLab) filteredLabs[key] = obj;
if (includeThisLab) filteredLabStatusesByLab[lab] = status;

if (stop != NULL) *stop = NO;
return;
}];

NSArray* sortedLabs = [self sortedLabsFrom:[filteredLabs allKeys]];
NSArray *sortedLabStatusesByLab = [self sortedLabsFrom:[filteredLabStatusesByLab allKeys]];

self.statusForTableViewSection = nil;
NSMutableDictionary *labsByStatus = [NSMutableDictionary dictionary];

for (id lab in sortedLabs) {
DZCLabStatus status = [(NSNumber *)filteredLabs[lab] intValue];
for (id lab in sortedLabStatusesByLab) {
DZCLabStatus status = [(NSNumber *)filteredLabStatusesByLab[lab] integerValue];

NSMutableArray *labs = self.labsByStatus[@(status)];
if (!labs) {
self.labsByStatus[@(status)] = [NSMutableArray array];
labs = self.labsByStatus[@(status)];
NSMutableArray *labsWithThisStatus = labsByStatus[@(status)];
if (!labsWithThisStatus) {
labsWithThisStatus = [NSMutableArray array];
labsByStatus[@(status)] = labsWithThisStatus;
}

[labs addObject:lab];
[labsWithThisStatus addObject:lab];
}

NSMutableArray *statusForTableViewSection = [NSMutableArray array];

for (DZCLabStatus i=0; i<DZCLabStatusCount; ++i) {
if (self.labsByStatus[@(i)] != nil) {
[self.statusForTableViewSection addObject:@(i)];
[statusForTableViewSection addObject:@(i)];
}
}

self.labsByStatus = labsByStatus;
self.statusForTableViewSection = statusForTableViewSection;
[self.tableView reloadData];
}];
}
Expand Down Expand Up @@ -442,18 +442,18 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

#pragma mark - Property overrides

- (NSMutableDictionary *)labsByStatus
- (NSDictionary *)labsByStatus
{
if(!_labsByStatus) {
_labsByStatus = [NSMutableDictionary dictionaryWithCapacity:5];
_labsByStatus = @{};
}
return _labsByStatus;
}

- (NSMutableArray *)statusForTableViewSection
- (NSArray *)statusForTableViewSection
{
if (!_statusForTableViewSection) {
_statusForTableViewSection = [NSMutableArray array];
_statusForTableViewSection = @[];
}
return _statusForTableViewSection;
}
Expand Down

0 comments on commit 4081e37

Please sign in to comment.