Chen Wei Chen Wei - 3 months ago 10
Objective-C Question

iOS continuous action

The basic scenario is to request data from server once some

parameters
that represented by tags(
UIView
) has been edited by user. The general solution is to add events to observe those tags. Once any tag has been removed, a new http request could be submitted & then comes problem. For example, if user continuous deleted two tags, the original solution would submit two request continuously. It's definitely irrational as the first request is redundant.

So, is there any idea to regard those continuous action as a single operation?

Answer

Em, the solution here I found was NSTimer. By using that, I could set an auto-executed method which observing a count down value. Once that value count to zero, some actions could be trigged. If user continuously changed those tags, that count down value would be initialized repeatedly.

-(void)countDownTimer{
if(!timer){
    timer = [NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
}
 timeLeft=2;

}

- (void)updateCounter:(NSTimer *)theTimer {
if(timeLeft > 0 ){
    timeLeft--;
    NSLog(@"%d",timeLeft);
}
else{
    //trigger update
    //balabala
    [timer invalidate];
    timer = nil;
}

}