Reputation: 49344
Is it possible to observe a property of an object that is yet to be set?
I am trying to implement MVVM pattern and here's what I've got so far:
In my view controller I instantiate view model and model:
- (void)viewDidLoad
{
[super viewDidLoad];
Model * model = [Model initWithSomeData:[self dataFromSomewhere]];
ViewModel * viewModel = [[ViewModel alloc] init];
viewModel.model = model;
RAC(self.propertyLabel, text) = [RACObserve(self.viewModel, someTransformedProperty];
}
Now in ViewModel class I have following code in initialiser
- (instancetype)init
{
self = [super init];
if (self) {
RAC(self, someTransformedProperty = [RACObserve(self.model, plainProperty) map:id^(id value) {
return [self transformProperty:value];
}];
}
}
Now the problem is that property label does not get set. Is there any way around this? Also - if plainProperty
in model
is nil I get a crash even if I provide nilValue
as 3rd parameter to RAC
macro.
Any help is much appreciated.
Upvotes: 0
Views: 114
Reputation: 22403
Yeah! Just turn RACObserve(self.model, plainProperty)
into RACObserve(self, model.plainProperty)
.
Oh but also:
ViewModel * viewModel = [[ViewModel alloc] init];
viewModel.model = model;
self.viewModel = viewModel; // <--- don't forget that
RAC(self.propertyLabel, text) = RACObserve(self.viewModel, someTransformedProperty);
As far as the crash goes, hard to say what that could be without more info. Is transformProperty:
crashing on nil
?
Upvotes: 2