MCMatan
MCMatan

Reputation: 8843

Reactivecocoa doesn't recognize new instance changes

Say I have a View that is bonded to his ViewModel like so:

View:

RAC(self.lbl, userName) = RACObserve(self.viewModel.user, userName);

This will work great! but what happens in cases when I load a new User instance and set it like so:

ViewModel:

-(void) userUpdated: (User *) user {

self.user =  user;

}

In this case, the views binding will still be bonded to the old user instance! Did any one come across this and find a better way to solve this except setting all properties of the old user with the new ones...?

Thanks!!

Upvotes: 1

Views: 86

Answers (1)

Ian Henry
Ian Henry

Reputation: 22403

RAC(self.lbl, userName) = RACObserve(self.viewModel, user.userName);

The comma denotes the break between the "static" part (self.viewModel) and the dynamic, changing part (user.userName).

This is a really nice feature of RACObserve, but you could implement this yourself, to modify Leo's answer so that it works with a changing userName: map not into the username, but into a signal of usernames, and then "flatten" that with switchToLatest:

RAC(self.lbl, userName) = [[RACObserve(self.viewModel, user) map:^(User *user) {
    return RACObserve(user, userName);
}] switchToLatest];

This simple example isn't very useful, since RACObserve has this built in, but this technique in general is extremely powerful. You will eventually want to map into signals of not-observed-things, so I encourage you to take the time to understand why this works.

Upvotes: 1

Related Questions