It is easy to delay executing something like this
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 to be executed on the main queue after delay#>
});
But it will make above code fail to execute if putting another longer delay like
double delayInSeconds2 = 3.0;
dispatch_time_t popTime2 = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds2 * NSEC_PER_SEC);
dispatch_after(popTime2, dispatch_get_main_queue(), ^(void){
<#code to be executed on the main queue after delay#>
});
Why just execute the longer one instead of both ? Or am I totally wrong ?
it is a queue, but the position depend on the future fire time instead of enqueue time.
if second one is 1 second, it will be insert before the first one.
if you want them executed concurrently (time slice for single core or real concurrency for multi-core cpu), put them on different queue.
Related
I need to download an image asynchronously without blocking the UI in an iOS app. While downloading the image, a 'waiting' UIView must be shown for 3 seconds at least. I would like to implement a solution that does not block UI management (even if in the current implementation no user operations are offered while the image download is in progress).
The current solution is:
- main thread: dispatch_async + block to download the image (described in thread_2);
- main thread: sleep for three seconds;
- main thread: P (wait) on a semaphore S;
- main thread: read data or error message set by thread_2, then behave accordingly.
- thread_2: download the image, set data or error flag/msg according to the download result;
- thread_2: V (signal) on the semaphore S.
There are other solutions, for example based on NSNotification, but this one seems the best for respecting the 3-seconds delay.
My question is: when the main thread is sleeping (or when it is waiting on the semaphore), is the UI frozen? If it is, which solution would be the best one?
What do you think of this second one:
- main thread: dispatch_async + block to download the image (described in thread_2);
- main thread: dispath_async thread_3
- thread_2: as above
- thread_3: sleep three seconds, P on semaphore S;
- thread_3: read data or error message set by thread_2, prepare everything, then behave accordingly using the main_queue.
This is a way working with multiple threads and with specific delay
double delayInSeconds =3;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
//Entering a specific thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
//Wait delayInSeconds and this thread will dispatch
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//Do your main thread operations here
});
});
More or less you can do this:
dispatch_async(your_download_queue, ^{
dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC);
//download image
dispatch_after(timer, dispatch_get_main_queue(), ^{
//update ui with your image
});
});
I got stuck in a problem. I'm trying to run a timer in a method which itself is in background resulting my timer is not initiating. I got to know some where that timer can't be initialized in background so is there any way to do this?
You can perform the operation on main thread.
[self performSelectorOnMainThread:#selector(initTimer) withObject:nil waitUntilDone:NO];
and then
- (void)initTimer {
// Init your timer here.
}
You can try Background Fetch in ios7 to run code on background
Try this :
// provide value as required. Time here is 3 sec
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// do your task
});
As pointed out by #Bryan Chen, you can schedule a method to be run on the current thread using the following code:
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC);
dispatch_after(when, dispatch_get_current_queue(), ^(void) {
[self myTimedMethod];
});
You cannot use NSTimer in a background thread unless you are maintaining a run loop on that thread.
I want to call a method after certain time.
This is just an example.
- (void)myMethod:(int)value1 setValue2:(CGPoint)value2{
//Do Something with values
}
At first I thought
[self scheduleOnce:#selector(myMethod:setValue2:) delay:timeToWait];
but I can't pass the arguments when using selector, so Im asking you guys for an alternative...What could I do?
Thanks for your time guys and have a great day!
You can try GCD's dispatch_after. For instance:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self myMethod:someValue setValue2: someValue2];
});
You could put your arguments into an NSDictionary and pass that as the object parameter to - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
I'd like to pop a display from inside a completion block, but I also want to dismissModalViewControllerAnimated from inside the same block (but AFTER the popup completes). Note that the popup schedules and runs after the completion block execution finishes, which means it never happens since the dismissal is synchronous...
So, a quick fix would be to find a way to schedule the dismissal asynchronously for after the popup. Is there a chaining method? A way to force holding async tasks to run and wait for them?
Use -performSelector:withObject:afterDelay:. You'll need to wrap the dismissal in a selector matching the signature required by -performSelector:..., since dismissModalViewControllerAnimated: takes a BOOL.
You can use dispatch_after instead, and it will be dismissed animatedly. check this simple sample code:
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self dismissModalViewControllerAnimated:YES];
});
I have a need to delay for a certain amount of time and yet allow other things on the same runloop to keep running. I have been using the following code to do this:
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
This seems to do exactly what I want, except that sometimes the function returns immediately without waiting the desired time (1 second).
Can anyone let me know what could cause this? And what is the proper way to wait while allowing the run loop to run?
NOTE: I want to delay in a manner similar to sleep(), such that after the delay I am back in the same execution stream as before.
You should use GCD and dispatch_after for that. It is much more recent and efficient (and thread-safe and all), and very easy to use.
There is even a code snippet embedded in Xcode, so that if you start typing dispatch_after it will suggest the snippet and if you validate it will write the prepared 2-3 lines for you in your code :)
int64_t 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 to be executed on the main queue after delay#>
});
Use an NSTimer to fire off a call to some method after a certain delay.
Have you tried performSelector:withObject:afterDelay:?
From the Apple documentation
Invokes a method of the receiver on the current thread using the default mode after a delay.
I had a similar issue and this is my solution. Hope it works for others as well.
__block bool dispatched = false;
while ( put your loop condition here )
{
if (dispatched)
{
// We want to relinquish control if we are already dispatched on this iteration.
[ [ NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate:[ NSDate date ] ];
continue;
}
// mark that a dispatch is being scheduled
dispatched = true;
int64_t delayInNanoSeconds = (int64_t) (0.1 * (float) NSEC_PER_SEC);
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInNanoSeconds);
dispatch_after(delayTime, dispatch_get_main_queue(), ^() {
// Do your loop stuff here
// and now ready for the next dispatch
dispatched = false;
} );
} // end of while