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

RACObserving an NSMutableArray #1197

Closed
fatuhoku opened this Issue Mar 24, 2014 · 7 comments

Comments

Projects
None yet
6 participants
@fatuhoku

fatuhoku commented Mar 24, 2014

Basically, I'm trying to do #772 but I call reloadData whenever a NSMutableArray is edited.

I'm read that NSMutableArray doesn't fire off any KVO signals and so RAC cannot react to them.

The question is, then, what's the workaround?

@joshaber

This comment has been minimized.

Show comment
Hide comment
@joshaber

joshaber Mar 24, 2014

Member

Do you need to know how the array changed, or just that it changed? If you're just calling -reloadData regardless, then just replace the array and observe the property:

[RACObserve(self, contents) subscribeNext:^(id _) {
    [tableView reloadData];
}];

self.contents = @[ /* new stuff */ ];
Member

joshaber commented Mar 24, 2014

Do you need to know how the array changed, or just that it changed? If you're just calling -reloadData regardless, then just replace the array and observe the property:

[RACObserve(self, contents) subscribeNext:^(id _) {
    [tableView reloadData];
}];

self.contents = @[ /* new stuff */ ];
@fatuhoku

This comment has been minimized.

Show comment
Hide comment
@fatuhoku

fatuhoku Mar 24, 2014

@joshaber I mean, I would like a signal that produces a value every time an object is added or deleted to or from the array.

fatuhoku commented Mar 24, 2014

@joshaber I mean, I would like a signal that produces a value every time an object is added or deleted to or from the array.

@notxcain

This comment has been minimized.

Show comment
Hide comment
@notxcain

notxcain Mar 24, 2014

Please show some of your code which leads to addition / deletion.

notxcain commented Mar 24, 2014

Please show some of your code which leads to addition / deletion.

@Coneko

This comment has been minimized.

Show comment
Hide comment
@Coneko

Coneko Mar 24, 2014

Member

I would like a signal that produces a value every time an object is added or deleted to or from the array.

That would be -rac_valuesAndChangesForKeyPath:options:observer:, but you have to make sure you change your contents property in KVO compliant ways.

The easiest way to do that is to use -mutableArrayValueForKey::

// WRONG! Will not send values from RAC signals
[self.contents addObject:object];
// Right! Will send values from RAC signals
NSMutableArray *contents = [account mutableArrayValueForKey:@keypath(self, contents)];
[contents addObject:object];

You can get more details about this, and how to implement faster ways of changing arrays and sets in a KVO compliant way, in the KVO programming guide: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVOCompliance.html#//apple_ref/doc/uid/20002178-BAJEAIEE

Member

Coneko commented Mar 24, 2014

I would like a signal that produces a value every time an object is added or deleted to or from the array.

That would be -rac_valuesAndChangesForKeyPath:options:observer:, but you have to make sure you change your contents property in KVO compliant ways.

The easiest way to do that is to use -mutableArrayValueForKey::

// WRONG! Will not send values from RAC signals
[self.contents addObject:object];
// Right! Will send values from RAC signals
NSMutableArray *contents = [account mutableArrayValueForKey:@keypath(self, contents)];
[contents addObject:object];

You can get more details about this, and how to implement faster ways of changing arrays and sets in a KVO compliant way, in the KVO programming guide: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVOCompliance.html#//apple_ref/doc/uid/20002178-BAJEAIEE

@fatuhoku

This comment has been minimized.

Show comment
Hide comment
@fatuhoku

fatuhoku Mar 25, 2014

Thanks @Coneko, looks like a great solution! I knew there probably was a 2-line solution to this, but I wouldn't have known where to look.

fatuhoku commented Mar 25, 2014

Thanks @Coneko, looks like a great solution! I knew there probably was a 2-line solution to this, but I wouldn't have known where to look.

@fatuhoku

This comment has been minimized.

Show comment
Hide comment
@fatuhoku

fatuhoku Apr 3, 2014

Okay, so I've gone more down the Core Data route now. I'm happy to close this until the issue arises again later.

fatuhoku commented Apr 3, 2014

Okay, so I've gone more down the Core Data route now. I'm happy to close this until the issue arises again later.

@fatuhoku fatuhoku closed this Apr 3, 2014

@haifengkao

This comment has been minimized.

Show comment
Hide comment
@haifengkao

haifengkao Jun 5, 2016

You can use KVOMutableArray to generate signal for NSMutableArray

haifengkao commented Jun 5, 2016

You can use KVOMutableArray to generate signal for NSMutableArray

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