ReactiveCocoa / ReactiveViewModel

Model-View-ViewModel, using ReactiveCocoa
MIT License
1.96k stars 259 forks source link

retain cycle when using didBecomeActiveSignal #43

Open adriantofan opened 9 years ago

adriantofan commented 9 years ago

Hello,

I have a retain cycle in my code caused by the following setup in viewDidLoad

   [[[[RACSignal
    combineLatest:@[RACObserve(self.viewModel,index),
/*A*/                   self.viewModel.didBecomeActiveSignal]]
/*B                          RACObserve(self.viewModel,active)]]*/
    deliverOnMainThread]
    distinctUntilChanged]
    subscribeNext:^(RACTuple* x) {

The option A is causing a retain cycle so the view model is never released. B works fine. Do you see something wrong with the code or it might be a bug ?

Thank you, Adrian

jspahrsummers commented 9 years ago

That sounds like a bug, if true. Could you please distill it down into a minimal test case? :pray:

adriantofan commented 9 years ago

Yes, Here is the code:

@interface ViewController:UIViewController
@property (nonatomic,strong,readwrite) RVMViewModel* viewModel;
@end

@implementation ViewController
-(void)viewDidLoad{
  [super viewDidLoad];
  self.viewModel = [[RVMViewModel alloc] init];
  [[[[RACSignal
    combineLatest:@[
/*A */                         self.viewModel.didBecomeActiveSignal]]
/*B                            RACObserve(self.viewModel,active)]] */
    deliverOnMainThread]
    distinctUntilChanged]
    subscribeNext:^(RACTuple* x) {
      NSLog(@"...");
    }];
  self.viewModel.active = YES;
}
@end

I would be happy to investigate further if you can give me any pointer about what might be wrong.

Thank you, Adrian

ashfurrow commented 9 years ago

@adriantofan Is that a RACObserve inside another RACObserve? That may be the source of the issue.

adriantofan commented 9 years ago

@ashfurrow no, that was a typo. Sorry about that

jspahrsummers commented 9 years ago

Ah, so, didBecomeActiveSignal will send the view model whenever a change occurs. That won't automatically cause a cycle, but it could result in problems if you replay the signal (thus keeping the view model alive), or capture the sent value in a block or something.