New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observing the count of a collection? #500

Closed
bogardon opened this Issue May 16, 2013 · 6 comments

Comments

Projects
None yet
4 participants
@bogardon

bogardon commented May 16, 2013

How would one observe the count of say a NSMutableArray with ReactiveCocoa? I have a UILabel that just needs to reflect the count at all times.

I tried using RACAble(self.array) subscribeNext..., but this will only fire if I set a completely new array instead of responding to insertions and removals.

Thanks in advance!

@jspahrsummers

This comment has been minimized.

Show comment
Hide comment
@jspahrsummers

jspahrsummers May 16, 2013

Member

This sounds like the same issue mentioned in this Stack Overflow comment thread.

Basically, since RACAble is just glorified KVO, your array property has to be Key-Value Observing compliant in order for insertions, removals, and replacements to generate a notification.

Member

jspahrsummers commented May 16, 2013

This sounds like the same issue mentioned in this Stack Overflow comment thread.

Basically, since RACAble is just glorified KVO, your array property has to be Key-Value Observing compliant in order for insertions, removals, and replacements to generate a notification.

@bogardon

This comment has been minimized.

Show comment
Hide comment
@bogardon

bogardon May 16, 2013

Understood! Thanks again!

bogardon commented May 16, 2013

Understood! Thanks again!

@bogardon bogardon closed this May 16, 2013

@jspahrsummers

This comment has been minimized.

Show comment
Hide comment
@jspahrsummers

jspahrsummers May 16, 2013

Member

No problem!

Member

jspahrsummers commented May 16, 2013

No problem!

@fatuhoku

This comment has been minimized.

Show comment
Hide comment
@fatuhoku

fatuhoku Mar 24, 2014

@jspahrsummers so, being absolutely crystal clear about this, is there really no way of detecting changes to an NSMutableArray reactively?

If you really had to, how would you do it?

fatuhoku commented Mar 24, 2014

@jspahrsummers so, being absolutely crystal clear about this, is there really no way of detecting changes to an NSMutableArray reactively?

If you really had to, how would you do it?

@notxcain

This comment has been minimized.

Show comment
Hide comment
@notxcain

notxcain Mar 24, 2014

@fatuhoku

Here is my experimental implementation. But I do not use it, it feels like some architectural smell.
RFChanges() is just a convenient macro for KVO, implemented through -rac_valuesAndChangesForKeyPath:options:observer:, and sending only changes.

@interface RFMutableArray : NSMutableArray
/// Returns a signal which will send KVO change dictionaries
@property (nonatomic, strong, readonly) RACSignal *changes;
@end

// We need this proxy because we can't add observer to NSArray.
@interface RFKVONotifyingProxy : NSObject
@property (nonatomic, strong, readonly) NSArray *array;
@property (nonatomic, strong, readonly) NSMutableArray *mutableArray;
- (id)initWithCapacity:(NSUInteger)capacity;
@end

@interface RFMutableArray ()
@property (nonatomic, strong, readonly) RFKVONotifyingProxy *proxy;
@end

@implementation RFMutableArray
- (id)init
{
    return [self initWithCapacity:0];
}

- (id)initWithCapacity:(NSUInteger)numItems
{
    self = [super init];
    if (self) {
        _proxy =  [[RFKVONotifyingProxy alloc] initWithCapacity:numItems];;
        _changes = RFChanges(self.proxy, array);
    }
    return self;
}

- (NSMutableArray *)targetArray
{
    return [self.proxy mutableArray];
}

- (NSUInteger)count
{
    return [self.targetArray count];
}

- (void)addObjectsFromArray:(NSArray *)otherArray
{
    [self.targetArray insertObjects:otherArray atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(self.count, otherArray.count)]];
}

- (id)objectAtIndex:(NSUInteger)index
{
    return [self.targetArray objectAtIndex:index];
}

- (void)addObject:(id)anObject
{
    [self.targetArray addObject:anObject];
}

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
{
    [self.targetArray insertObject:anObject atIndex:index];
}

- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes
{
    [self.targetArray insertObjects:objects atIndexes:indexes];
}

- (void)removeLastObject
{
    [self.targetArray removeLastObject];
}

- (void)removeObjectAtIndex:(NSUInteger)index
{
    [self.targetArray removeObjectAtIndex:index];
}

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{
    [self.targetArray replaceObjectAtIndex:index withObject:anObject];
}
@end

@implementation RFKVONotifyingProxy {
    NSMutableArray *_array;
}

- (id)initWithCapacity:(NSUInteger)capacity
{
    self = [super init];
    if (self) {
        _array = [[NSMutableArray alloc] initWithCapacity:capacity];
    }
    return self;
}

- (NSMutableArray *)mutableArray
{
    return [self mutableArrayValueForKey:@keypath(self.array)];
}

- (NSArray *)array
{
    return _array;
}
@end

notxcain commented Mar 24, 2014

@fatuhoku

Here is my experimental implementation. But I do not use it, it feels like some architectural smell.
RFChanges() is just a convenient macro for KVO, implemented through -rac_valuesAndChangesForKeyPath:options:observer:, and sending only changes.

@interface RFMutableArray : NSMutableArray
/// Returns a signal which will send KVO change dictionaries
@property (nonatomic, strong, readonly) RACSignal *changes;
@end

// We need this proxy because we can't add observer to NSArray.
@interface RFKVONotifyingProxy : NSObject
@property (nonatomic, strong, readonly) NSArray *array;
@property (nonatomic, strong, readonly) NSMutableArray *mutableArray;
- (id)initWithCapacity:(NSUInteger)capacity;
@end

@interface RFMutableArray ()
@property (nonatomic, strong, readonly) RFKVONotifyingProxy *proxy;
@end

@implementation RFMutableArray
- (id)init
{
    return [self initWithCapacity:0];
}

- (id)initWithCapacity:(NSUInteger)numItems
{
    self = [super init];
    if (self) {
        _proxy =  [[RFKVONotifyingProxy alloc] initWithCapacity:numItems];;
        _changes = RFChanges(self.proxy, array);
    }
    return self;
}

- (NSMutableArray *)targetArray
{
    return [self.proxy mutableArray];
}

- (NSUInteger)count
{
    return [self.targetArray count];
}

- (void)addObjectsFromArray:(NSArray *)otherArray
{
    [self.targetArray insertObjects:otherArray atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(self.count, otherArray.count)]];
}

- (id)objectAtIndex:(NSUInteger)index
{
    return [self.targetArray objectAtIndex:index];
}

- (void)addObject:(id)anObject
{
    [self.targetArray addObject:anObject];
}

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
{
    [self.targetArray insertObject:anObject atIndex:index];
}

- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes
{
    [self.targetArray insertObjects:objects atIndexes:indexes];
}

- (void)removeLastObject
{
    [self.targetArray removeLastObject];
}

- (void)removeObjectAtIndex:(NSUInteger)index
{
    [self.targetArray removeObjectAtIndex:index];
}

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{
    [self.targetArray replaceObjectAtIndex:index withObject:anObject];
}
@end

@implementation RFKVONotifyingProxy {
    NSMutableArray *_array;
}

- (id)initWithCapacity:(NSUInteger)capacity
{
    self = [super init];
    if (self) {
        _array = [[NSMutableArray alloc] initWithCapacity:capacity];
    }
    return self;
}

- (NSMutableArray *)mutableArray
{
    return [self mutableArrayValueForKey:@keypath(self.array)];
}

- (NSArray *)array
{
    return _array;
}
@end
@jspahrsummers

This comment has been minimized.

Show comment
Hide comment
@jspahrsummers

jspahrsummers Mar 24, 2014

Member

@fatuhoku Implement the proper KVO-compliance methods, as I detailed above.

Member

jspahrsummers commented Mar 24, 2014

@fatuhoku Implement the proper KVO-compliance methods, as I detailed above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment