ReactiveCocoa / ReactiveObjC

The 2.x ReactiveCocoa Objective-C API: Streams of values over time
MIT License
2.6k stars 496 forks source link

How to delete notification after rac_addObserverForName:object:? #168

Closed wMellon closed 4 years ago

wMellon commented 4 years ago

I put the following code in ViewController. When I close the ViewController and open again, then the block will execute more than once When the notification post. Only when I restart the APP and open this ViewController first time, the block execute once. Why? Can I program to delete it when I close the ViewController(example, in the dealloc method)?

- (void)viewDidLoad {
    [super viewDidLoad];
    @weakify(self)
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"MakeDoctorOrderNetRequest" object:self] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self)
        //block
    }];
}
shibo0305 commented 4 years ago

try this: @weakify(self) [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"MakeDoctorOrderNetRequest" object:self] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) { @strongify(self) //block }];

faimin commented 4 years ago

@wMellon the block will execute more than once When the notification post is because of memory leak, add takeUntil:self.rac_willDeallocSignal method can resolve this question.

wMellon commented 4 years ago

@wMellon the block will execute more than once When the notification post is because of memory leak, add takeUntil:self.rac_willDeallocSignal method can resolve this question.

thanks

wMellon commented 4 years ago

try this: @weakify(self) [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"MakeDoctorOrderNetRequest" object:self] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) { @strongify(self) //block }];

thanks