Gcd7: execute tasks after GCD Delay

Source: Internet
Author: User

Function:

You want to specify a delay through GCD. After the delay, you can continue to execute the relevant code.

Solution:

Use the dispatch_after and dispatch_after_f Functions

Discussion:

With core Foundation, you can call the selector in the object after a given period of time. This call can use the nsobject class Optional mselector: withobject: afterdelay:Methods. In GCD, we can use the dispatch_after and dispatch_after_f functions to achieve the same purpose. The two functions are described as follows:

Dispatch_afterAfter a given time period in nanoseconds, block objects are distributed to a dispatch queue. Parameters required for this function include:

Delay in nanoseconds

Before running the specified block object, gcd must wait for the number of nanoseconds in a given dispatch Queue (specified by the second parameter.

Dispatch queue

After a given delay (specified by the first parameter), the block object must be executed on the delivery queue.

Block object

After waiting for a certain number of seconds on the specified dispatch queue, the block object will be called. It does not return a value and does not take any parameters.

  Dispatch_after_fAssign a C function to GCD and execute it after a limited time in nanoseconds. This function accepts four parameters:

Delay in nanoseconds

The number of seconds to wait.

Dispatch queue

The C function must execute the dispatch queue on the given delay.

Context
The memory address of a value passed to the C function in the heap.
C Functions
The C function that must be executed after a specific period of time.

 

The following is an example of dispatch_after:

-(Void) dispatchafter {double delayinseconds = 2.0; dispatch_time_t delayinnanoseconds =Dispatch_time (dispatch_time_now, delayinseconds *Nsec_per_sec); Dispatch_queue_t concurrentqueue = dispatch_get_global_queue (queue, 0); dispatch_after (delayinnanoseconds, concurrentqueue, ^ {nslog (@ "delayed execution ");});}
For the dispatch_after and dispatch_after_f functions, the latency parameter must be a class of dispatch_time_t, which is an abstract representation of absolute time. To get the value of this parameter, you must use the dispatch_time function shown in this Code:

Dispatch_time (dispatch_time_t when, int64_t delta)

For example, it indicates a time of 3 seconds from now on. You can write your code like this:
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 3.0f*NSEC_PER_SEC);

 

Now let's take a look at how to use the dispatch_after_f function:

void processSomething(void *paramContext){    /* Do your processing here */    NSLog(@"Processing...");}- (void)dispatchAfterf{    dispatch_time_t delayInNanoSenconds = dispatch_time(DISPATCH_TIME_NOW, 2.0f*NSEC_PER_SEC);    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_after_f(delayInNanoSenconds, concurrentQueue, NULL, processSomething);}

 

 

 

 

Gcd7: execute tasks after GCD Delay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.