below i am defining a solution and using interval as a timer in background thread as follow :
@weakify(self)
//IMPORTANT:- Throttle is working exactly the same way debounce works in RX SO DO NOT USE IT.
RACScheduler *bacgroundScheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground];
RACSignal *sampler = [RACSignal interval:3 onScheduler: bacgroundScheduler];
enter code here
// updateListenerPositionSubject is a RACReplaySubject.
RACSignal *fallbackSignal = [[RACSignal
merge:@[ self.updateListenerPositionSubject, sampler ]]
takeUntil:[self.updateListenerPositionSubject ignoreValues]];
@weakify(self);
[fallbackSignal subscribeNext:^(id _Nullable x) {
@strongify(self);
[self solutionFallBack];
} error:^(NSError *error) {
NSLog(@"Error: %@", error);
} completed:^{
// to make sure subscription get completed when updateListenerPositionSubject sends complete.
NSLog(@"Completed");
}];
}
And solutionFallBack function is defined as follow:
-(void) solutionFallback {
// block the original solution.
[self.updateListenerPositionSubject sendCompleted];
// bunch of conditions
[self performSwitchWith:shape];
}
In case "solution Fallback" condition satisfied the viewmodel will be deallocated after a while (maybe 30sec or 1min) which is not good specially i am doing unloadings in dealloc.
So I have tried different solution to avoid have "sample" & "ipdateListenerPositionSubject" in different threads, I have tried to subscribe to sample signal and takeuntil fallback condition satisfied as follow :
RACScheduler *bacgroundScheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground];
RACSignal *rac_viewModelWillDealloc = [self rac_signalForSelector:@selector(performSwitchWith:)];
RACSignal *sampler = [[RACSignal interval:self.sceneSwitchConfiguration.roundDuration onScheduler:bacgroundScheduler] takeUntil: rac_viewModelWillDealloc];
@weakify(self);
[sampler subscribeNext:^(id _Nullable x) {
@strongify(self);
self.backgroundThread = [NSThread currentThread];
[self solutionFallback];
} error:^(NSError *error) {
NSLog(@"Error: %@", error);
} completed:^{
NSLog(@"Completed");
}];
and when i make sure solutoionFallback solution condition satisfied which is calling "performSwitchWith"... i am cancelling current background thread and handover to another as follow:
@weakify(self);
[self.backgroundThread cancel];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
@strongify(self);
// continue init the new vm process here.
});
So when I switch the interval to be scheduled in Main thread all works as expected, and dealloc take a place instantly :
RACSignal *sampler = [[RACSignal
interval:self.sceneSwitchConfiguration.roundDuration onScheduler:[RACScheduler mainThreadScheduler]] takeUntil:rac_viewModelWillDealloc];
I want to keep the sampler signal in background thread and to have the class deallocated immediately.