2

How do I subscribe to objects being added and removed from a NSMutableDictionary using ReactiveCocoa? Also, I'd like to broadcast a notification when it changes. My guess is that broadcasting can be done using RACMulticastConnection but how do I tie this up with the dictionary change? I'm trying to use ReactiveCocoa for the first time in my project and stuck on the first thing I wanted to do :(

Kara
  • 6,115
  • 16
  • 50
  • 57
ashokgelal
  • 80,002
  • 26
  • 71
  • 84

2 Answers2

7

RACObserve is a wrapper around key-value observing, and inherits the same features and flaws.

Unfortunately, NSMutableDictionary is not automatically observable. There are two ways to work around that:

  1. Subclass it and add KVO support.
  2. Create a real model object, with properties instead of dictionary keys. Then you'll get KVO on those properties, as long as you use setters instead of direct ivar modification.

I'm not sure exactly what you mean by "[broadcasting] a notification when it changes," or why it'd be valuable. Notifications are way too global for my taste, and I'd promote using more limited observation instead (like KVO).

However, assuming you definitely want to do this, it's simple enough to post a notification in response to a new signal value:

@weakify(self);
[RACObserve(self, dictionary) subscribeNext:^(NSDictionary *dictionaryValue) {
    @strongify(self);
    [NSNotificationCenter.defaultCenter postNotificationName:SomeNotificationName object:self];
}];

If you want KVO's change dictionary (which includes information about the added/removed values), you'll need to replace RACObserve with +rac_valuesAndChangesForKeyPath:options:observer:.

Community
  • 1
  • 1
Justin Spahr-Summers
  • 16,893
  • 2
  • 61
  • 79
  • Thanks! I ended up using a real model object. The broadcasting question was more about curiosity than anything else. I do need a global broadcast and ended up using NSNotificationCenter. On the observing side, I'm using addObserverForName:object:queue:usingBlock: On a side note, it really feels the documentation on ReactiveCocoa is lacking. Someone needs to write a book on it. Probably the creators of the library themselves :) – ashokgelal Oct 07 '13 at 17:32
  • @cod3-monk-3y Have you checked out the headers? Each method and class is documented quite exhaustively. There's also an entire [Documentation](https://github.com/ReactiveCocoa/ReactiveCocoa/tree/master/Documentation) folder that explains a lot of higher-level stuff. – Justin Spahr-Summers Oct 07 '13 at 18:19
  • I actually have and to be honest, they are really good. But they are also disconnected. They are good at explaining what they are but not on how to use them or how two different things solve a problem together. To get better at it, I've gone through .net ReactiveExtensions as well as, as suggested by you somewhere, elm programming language. But I think the world needs better/real ReactiveCocoa examples. Most of them seem to just show how to bind a textfield or a button. One that I've really liked is this one: http://stackoverflow.com/a/14072445/33203 Blows my mind, seriously! - my 2 cents – ashokgelal Oct 07 '13 at 18:53
  • Also, the GHAPIDemo is also an awesome resource for beginners. https://github.com/ReactiveCocoa/GHAPIDemo – ashokgelal Oct 07 '13 at 18:56
  • The link to GHAPIDemo is broken. – fatuhoku Feb 03 '15 at 10:04
-1

every time you set or remove the key-value,reset the dict,so you can observer the dict. just like:

 [RACObserve(self, testDict) subscribeNext:^(id x) {
     NSLog(@"RACObserve testDict:%@",x);
 }];

[self.testDict setObject:value forKey:key];
self.testDict=self.testDict;
user3044484
  • 463
  • 5
  • 7