Skip to content

Commit

Permalink
upate the lib interface
Browse files Browse the repository at this point in the history
  • Loading branch information
David Fu committed Jul 29, 2015
1 parent 45bd816 commit ef310e1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
18 changes: 7 additions & 11 deletions Example/SMBFetchedResultsController/SMBViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ - (IBAction)insertButtonClick:(id)sender {

- (IBAction)removeButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 0) {
[self.fetchedResultsController.fetchedResults removeObject:[self.fetchedResultsController.fetchedResults.data lastObject]];
[self.fetchedResultsController.fetchedResults removeObject:[self.fetchedResultsController.fetchedResultsOrderedSet lastObject]];
}
else {
NSLog(@"Error!, remove operation should work under the condition count > 0");
Expand All @@ -107,7 +107,7 @@ - (IBAction)removeButtonClick:(id)sender {

- (IBAction)replaceButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 0) {
Person *person = [self.fetchedResultsController.fetchedResults.data firstObject];
Person *person = [self.fetchedResultsController.fetchedResultsOrderedSet firstObject];
person.name = @"mary";
person.age = rand()%20;
[self.fetchedResultsController.fetchedResults updateObject:person];
Expand All @@ -121,7 +121,7 @@ - (IBAction)replaceButtonClick:(id)sender {
- (IBAction)moveButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 1) {
NSUInteger lastIndex = self.fetchedResultsController.fetchedResults.countOfData - 1;
[self.fetchedResultsController.fetchedResults moveObjectInDataAtIndex:lastIndex toIndex:0];
[self.fetchedResultsController.fetchedResults moveObjectFromIndex:lastIndex toIndex:0];
}
else {
NSLog(@"Error!, replace operation should work under the condition count > 0");
Expand All @@ -132,19 +132,15 @@ - (IBAction)insertsButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 0) {
Person *person = [[Person alloc] initWithName:@"zoe"];
Person *person1 = [[Person alloc] initWithName:@"petter"];
NSMutableIndexSet *indexset = [NSMutableIndexSet indexSet];
[indexset addIndex:0];
[indexset addIndex:self.fetchedResultsController.fetchedResults.countOfData];
[self.fetchedResultsController.fetchedResults insertObjectsFromArray:@[person, person1]];
}
}

- (IBAction)removesButtonClick:(id)sender {
if (self.fetchedResultsController.fetchedResults.countOfData > 3) {
NSMutableIndexSet *indexset = [NSMutableIndexSet indexSet];
[indexset addIndex:1];
[indexset addIndex:2];
[self.fetchedResultsController.fetchedResults removeDataAtIndexes:indexset];
Person *person = [self.fetchedResultsController.fetchedResults objectInDataAtIndex:1];
Person *person1 = [self.fetchedResultsController.fetchedResults objectInDataAtIndex:2];
[self.fetchedResultsController.fetchedResults removeObjectsFromArray:@[person, person1]];
}
else {
NSLog(@"Error!, remove operation should work under the condition count > 0");
Expand All @@ -162,7 +158,7 @@ - (IBAction)replacesButtonClick:(id)sender {
NSMutableIndexSet *indexset = [NSMutableIndexSet indexSet];
[indexset addIndex:0];
[indexset addIndex:1];
[self.fetchedResultsController.fetchedResults replaceDataAtIndexes:indexset withData:@[person, person1]];
[self.fetchedResultsController.fetchedResults updateObjectsFromArray:@[person, person1]];
}
else {
NSLog(@"Error!, replace operation should work under the condition count > 0");
Expand Down
9 changes: 0 additions & 9 deletions Pod/Classes/SMBFetchedResults.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

@interface SMBFetchedResults : NSObject

@property (readonly, nonatomic, strong) NSMutableOrderedSet *data;

@property (readonly, nonatomic, copy) NSString *sortKeyPaths;

@property (readonly, nonatomic, assign) NSStringCompareOptions options;
Expand Down Expand Up @@ -52,11 +50,4 @@

- (void)moveObjectFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;

- (NSUInteger)indexOfObject:(id <SMBFetchedResultsProtocol>)anObject;


- (id <SMBFetchedResultsProtocol>)objectInDataAtIndex:(NSUInteger)index;
- (NSArray *)dataAtIndexes:(NSIndexSet *)indexes;
- (NSUInteger)countOfData;

@end
40 changes: 26 additions & 14 deletions Pod/Classes/SMBFetchedResults.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ - (void)replaceDataAtIndexes:(NSIndexSet *)indexes withData:(NSArray *)data;
/** custom KVC one-to-many compliance interface for move */
- (void)moveObjectInDataAtIndex:(NSUInteger)index toIndex:(NSUInteger)toIndex;

- (id <SMBFetchedResultsProtocol>)objectInDataAtIndex:(NSUInteger)index;
- (NSArray *)dataAtIndexes:(NSIndexSet *)indexes;
- (NSUInteger)countOfData;

@end

@implementation SMBFetchedResults
Expand All @@ -47,23 +51,35 @@ - (instancetype)initWithMutableData:(NSMutableOrderedSet *)mutableData {
}

- (instancetype)initWithMutableData:(NSMutableOrderedSet *)mutableData sortKeyPaths:(NSString *)sortKeyPaths sortOptions:(NSStringCompareOptions)options {
self = [super init];
if (self) {

_data = mutableData;
_sortKeyPaths = sortKeyPaths;
_options = options;
_moving = NO;
_queue = dispatch_queue_create("CTFetchecResults queue", NULL);
}
_data = mutableData;
_sortKeyPaths = sortKeyPaths;
_options = options;
_moving = NO;
_queue = dispatch_queue_create("CTFetchecResults queue", NULL);
return self;
}

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
if ([key isEqualToString:@"data"]) {
return YES;
}
return [super automaticallyNotifiesObserversForKey:key];
return NO;
//return [super automaticallyNotifiesObserversForKey:key];
}

#pragma mark Proxying

- (void)forwardInvocation:(NSInvocation *)anInvocation {
[anInvocation setTarget:self.data];
[anInvocation invoke];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [self.data methodSignatureForSelector:sel];
}

- (NSString *)description {
return [self.data description];
}

#pragma mark - delegate methods
Expand Down Expand Up @@ -275,8 +291,4 @@ - (NSUInteger)indexOfObject:(id)anObject {
return [_data indexOfObject:anObject];
}

- (NSString *)description {
return [_data description];
}

@end
2 changes: 2 additions & 0 deletions Pod/Classes/SMBFetchedResultsController.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ typedef NS_ENUM(NSUInteger, SMBFetchedResultsChangeType) {

@property (readonly, nonatomic, strong) SMBFetchedResults *fetchedResults;

@property (readonly, nonatomic, strong) NSMutableOrderedSet *fetchedResultsOrderedSet;

@property (readonly, nonatomic, copy) NSString *title;

- (instancetype)initWithFetchedResults:(SMBFetchedResults *)results title:(NSString *)title;
Expand Down
16 changes: 10 additions & 6 deletions Pod/Classes/SMBFetchedResultsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (void)adjustMovementForSortedOrderedSetAfterCurrentAction {
if (self.fetchedResults.sorted) {
NSArray *finalArray = [self.fetchedResults sortedResultWithOrderedSet:self.fetchedResults.data];
NSArray *finalArray = [self.fetchedResults sortedResultWithOrderedSet:self.fetchedResultsOrderedSet];
for (id <SMBFetchedResultsProtocol> object in finalArray) {
NSUInteger originIndex = [self.fetchedResults.data indexOfObject:object];
NSUInteger originIndex = [self.fetchedResultsOrderedSet indexOfObject:object];
NSUInteger finalIndex = [finalArray indexOfObject:object];
if (originIndex != finalIndex) {
[self.fetchedResults moveObjectFromIndex:originIndex toIndex:finalIndex];
Expand Down Expand Up @@ -182,22 +182,26 @@ - (void)notifyEndChanges {

#pragma mark - accessor methods

- (NSMutableOrderedSet *)fetchedResultsOrderedSet {
return (NSMutableOrderedSet *)self.fetchedResults;
}

#pragma mark - api methods

- (NSIndexPath *)indexPathForLastObject {
if (!self.fetchedResults.countOfData) {
if (!self.fetchedResultsOrderedSet.count) {
return nil;
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.fetchedResults.countOfData - 1 inSection:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.fetchedResultsOrderedSet.count - 1 inSection:0];
return indexPath;
}

- (id)objectAtIndexPath:(NSIndexPath *)indexPath {
return [self.fetchedResults objectInDataAtIndex:indexPath.row];
return [self.fetchedResultsOrderedSet objectAtIndex:indexPath.row];
}

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

0 comments on commit ef310e1

Please sign in to comment.