3

Let's say I want to execute a block of code later in time, so I call dispatch_after like so:

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    /* code */
});

But what if I want to "pause" my program before the execution even began? Let's say I want to pause the program 1 second after this call for unknown time. Then after the pause, I would like to resume the 2 second waiting on the queue. So it would look something like this:

  • call dispatch_after with a 2 second delay
  • after 1 second, pause the program for unknown time
  • after resuming, wait 1 second and execute the block (so the total delay is 2 secs)

Is there a way of doing that? Or should I use another approach?

I know that dispatch_suspend and dispatch_resume exist, but they don't really work for me (or I just don't know how to use them properly).

The solution doesn't necessarily have to involve a block, it could also be a delayed callback to a specified function. The point is that I want to be able to pause the waiting time until execution.

notadam
  • 2,754
  • 2
  • 19
  • 35
  • I'm pretty sure it's not possible. As far as I'm aware, once you've dispatched the block, there is no way to change the dispatch time on it. Suspending the queue won't affect the dispatch time either, it will just prevent the block from being run once the time is up. – Tom Dalling Sep 11 '14 at 05:02
  • So could you then suggest another approach? Maybe a delayed callback to a function? That would also be good. I actually want to use this in an animation sequence. First I animate something on the screen, then after 2 seconds I want to execute some code that also does something to it. It works great with `dispatch_after` but if you send the app to the background then bring it back (during the animation), the timing is screwed up. The code inside the block executes immediately instead of waiting for 2 secs. – notadam Sep 11 '14 at 05:05

2 Answers2

1

You can't achieve that, but there is a way to do the similar thing.

Delay your method 2 seconds :

[self performSelector:@selector(yourMethod) withObject:nil afterDelay:2.0];

1 second later, cancel your method :

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(yourMethod) object:nil];

After a unknown time, recall your method and delay to execute it 1 second later :

[self performSelector:@selector(yourMethod) withObject:nil afterDelay:1.0];

KudoCC
  • 6,912
  • 1
  • 24
  • 53
  • Not exactly what I need unfortunately. The 1 second - 1 second split was just an example. The interruption can happen any time in the 2 second window. I can't believe there's no way to pause the delay on a callback. – notadam Sep 11 '14 at 05:32
  • Maybe if I save the current time when the interruption happens, then calculate the elapsed time when resuming, I can simply check whether is there any time remaining, and call another performSelector thing with the remaining time. I will try that – notadam Sep 11 '14 at 05:34
0

Put your block in a method

-(void)myMethod{
//implementation of the block
}

where you want to call the block BUT a 2 second delay before it,

call

[self performSelector:@selector(myMethod) withObject:nil afterDelay:2.0];

Hope this helps

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35