Skip to content

Commit

Permalink
add indexOfObject safe check and update example
Browse files Browse the repository at this point in the history
  • Loading branch information
David Fu committed Jul 30, 2015
1 parent cdbdfb4 commit 3dfa303
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
24 changes: 12 additions & 12 deletions Example/SMBFetchedResultsController/SMBViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (IBAction)insertButtonClick:(id)sender {
}

- (IBAction)removeButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 0) {
if (self.fetchedResultsController.fetchedResultsOrderedSet.count > 0) {
[self.fetchedResultsController.fetchedResults removeObject:[self.fetchedResultsController.fetchedResultsOrderedSet lastObject]];
}
else {
Expand All @@ -106,7 +106,7 @@ - (IBAction)removeButtonClick:(id)sender {
}

- (IBAction)replaceButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 0) {
if (self.fetchedResultsController.fetchedResultsOrderedSet.count > 0) {
Person *person = [self.fetchedResultsController.fetchedResultsOrderedSet firstObject];
person.name = @"mary";
person.age = rand()%20;
Expand All @@ -119,8 +119,8 @@ - (IBAction)replaceButtonClick:(id)sender {
}

- (IBAction)moveButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 1) {
NSUInteger lastIndex = self.fetchedResultsController.fetchedResults.countOfData - 1;
if (self.fetchedResultsController.fetchedResultsOrderedSet.count > 1) {
NSUInteger lastIndex = self.fetchedResultsController.fetchedResultsOrderedSet.count - 1;
[self.fetchedResultsController.fetchedResults moveObjectFromIndex:lastIndex toIndex:0];
}
else {
Expand All @@ -129,17 +129,17 @@ - (IBAction)moveButtonClick:(id)sender {
}

- (IBAction)insertsButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 0) {
if (self.fetchedResultsController.fetchedResultsOrderedSet.count > 0) {
Person *person = [[Person alloc] initWithName:@"zoe"];
Person *person1 = [[Person alloc] initWithName:@"petter"];
[self.fetchedResultsController.fetchedResults insertObjectsFromArray:@[person, person1]];
}
}

- (IBAction)removesButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 3) {
Person *person = [self.fetchedResultsController.fetchedResults objectInDataAtIndex:1];
Person *person1 = [self.fetchedResultsController.fetchedResults objectInDataAtIndex:2];
if (self.fetchedResultsController.fetchedResultsOrderedSet.count > 3) {
Person *person = [self.fetchedResultsController.fetchedResultsOrderedSet objectAtIndex:1];
Person *person1 = [self.fetchedResultsController.fetchedResultsOrderedSet objectAtIndex:2];
[self.fetchedResultsController.fetchedResults removeObjectsFromArray:@[person, person1]];
}
else {
Expand All @@ -148,11 +148,11 @@ - (IBAction)removesButtonClick:(id)sender {
}

- (IBAction)replacesButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 1) {
Person *person = [self.fetchedResultsController.fetchedResults objectInDataAtIndex:0];
if (self.fetchedResultsController.fetchedResultsOrderedSet.count > 1) {
Person *person = [self.fetchedResultsController.fetchedResultsOrderedSet objectAtIndex:0];
person.name = @"mary";
person.age = rand()%20;
Person *person1 = [self.fetchedResultsController.fetchedResults objectInDataAtIndex:1];
Person *person1 = [self.fetchedResultsController.fetchedResultsOrderedSet objectAtIndex:1];
person1.name = @"tom";
person1.age = rand()%20;
NSMutableIndexSet *indexset = [NSMutableIndexSet indexSet];
Expand All @@ -169,7 +169,7 @@ - (IBAction)replacesButtonClick:(id)sender {
#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.fetchedResultsController.fetchedResults.countOfData;
return self.fetchedResultsController.fetchedResultsOrderedSet.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
22 changes: 16 additions & 6 deletions Pod/Classes/SMBFetchedResults.m
Original file line number Diff line number Diff line change
Expand Up @@ -254,43 +254,53 @@ - (void)insertObjectsFromArray:(NSArray *)otherArray {

- (void)removeObject:(id<SMBFetchedResultsProtocol>)object {
NSUInteger index = [_data indexOfObject:object];
if (index == NSNotFound) {
return;
}

[self removeObjectFromDataAtIndex:index];
}

- (void)removeObjectsFromArray:(NSArray *)otherArray {
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (id <SMBFetchedResultsProtocol> object in otherArray) {
NSUInteger index = [_data indexOfObject:object];
[indexSet addIndex:index];
if (index != NSNotFound) {
[indexSet addIndex:index];
}
}
[self removeDataAtIndexes:indexSet];
}

- (void)updateObject:(id)object {
NSUInteger index = [_data indexOfObject:object];
if (index == NSNotFound) {
return;
}
[self replaceObjectInDataAtIndex:index withObject:object];
}

- (void)updateObjectsFromArray:(NSArray *)otherArray {
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (id <SMBFetchedResultsProtocol> object in otherArray) {
NSUInteger index = [_data indexOfObject:object];
[indexSet addIndex:index];
if (index != NSNotFound) {
[indexSet addIndex:index];
}
}
[self replaceDataAtIndexes:indexSet withData:otherArray];
}

- (void)bubbleObject:(id)object {
NSUInteger index = [_data indexOfObject:object];
if (index == NSNotFound) {
return;
}
[self moveObjectInDataAtIndex:index toIndex:0];
}

- (void)moveObjectFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex {
[self moveObjectInDataAtIndex:fromIndex toIndex:toIndex];
}

- (NSUInteger)indexOfObject:(id)anObject {
return [_data indexOfObject:anObject];
}

@end
3 changes: 3 additions & 0 deletions Pod/Classes/SMBFetchedResultsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ - (id)objectAtIndexPath:(NSIndexPath *)indexPath {

- (NSIndexPath *)indexPathForObject:(id)object {
NSUInteger row = [self.fetchedResultsOrderedSet indexOfObject:object];
if (row == NSNotFound) {
return nil;
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
return indexPath;
}
Expand Down

0 comments on commit 3dfa303

Please sign in to comment.