ReactiveViewModel icon indicating copy to clipboard operation
ReactiveViewModel copied to clipboard

retain cycle when using didBecomeActiveSignal

Open adriantofan opened this issue 10 years ago • 5 comments

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

adriantofan avatar Apr 03 '15 14:04 adriantofan

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

jspahrsummers avatar Apr 06 '15 21:04 jspahrsummers

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

adriantofan avatar Apr 07 '15 08:04 adriantofan

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

ashfurrow avatar Apr 07 '15 11:04 ashfurrow

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

adriantofan avatar Apr 07 '15 11:04 adriantofan

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.

jspahrsummers avatar Apr 10 '15 18:04 jspahrsummers