I want to to perform method after 2.0 seconds, but i want to be able to cancel it if before the 5 seconds if another call made , but i noticed that the cancelPreviousPerformRequestsWithTarget send object nil and performSelector send object NSString.
Can it make a problems?
-(void)startRecord:(NSString*)name {
if (self.needToStartRecording) {
//Cancel last call
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRecordAfterDelay:) object:nil];
}
self.needToStartRecording = YES;
[self performSelector:@selector(startRecordAfterDelay:) withObject:name afterDelay:5.0];
}
-(void)startRecordAfterDelay:(NSString*)name {
self.needToStartRecording = NO;
//Do My Stuff
}
According to the documentation, if you pass nil, only a request performed with nil will be canceled. You should store somewhere the previous value.
The argument for requests previously registered with the performSelector:withObject:afterDelay: instance method. Argument equality is determined using isEqual:, so the value need not be the same object that was passed originally. Pass nil to match a request for nil that was originally passed as the argument.
Additionaly, if you have only this selector used with delay, you could call
[NSObject cancelPreviousPerformRequestsWithTarget:self]
This will cancel all previous request sent to self
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With