GCD Summary In April 11 (ii) I, After a certain number of latencies are specified, use GCD To executeCode-- Use Dispatch_after And Dispatch_after_f Function
1,Dispatch_after
After a given time period in nanosecondsBlock objectDistribute to a dispatch queue. This
Functions require the following parameters:
Delay in nanoseconds
In the execution of the specifiedBlock object (Specified by the third parameter)BeforeGCDMust be in a given dispatch queue(Specified by the second parameter)The number of seconds to wait.
Dispatch queue
Block object (Specified by the third parameter)Given latency(Specified by the first parameter)Then, you must run the dispatch queue on it.
Block object
Wait for a certain number of seconds on the specified dispatch queueBlock objectWill be called; it does not return values and does not accept parameters
Int64_tDelayinseconds = 20.0;
Dispatch_time_tPoptime =Dispatch_time(Dispatch_time_now, Delayinseconds *Nsec_per_sec);
Dispatch_after(Poptime,Dispatch_get_main_queue(), ^ (Void){
//
Nslog(@ "Oh fuck! ");
});
As you can seeDispatch_afterAndDispatch_after_fThe latency parameter of the function must be a class.Dispatch_time_tIt is an abstract representation of absolute time. To get the value of this parameter, you must useDispatch_timeFunction. Here you can passDispatch_time Function parameters:
Base time
Assume that the value isB,Delta ParameterThe value isDThe end time of the function is equalB + d. You can set the value of this parameterDispatch_time_nowTo use the current time as the base time, and then useDelta Parameter to determineDelta.
Delta to add to base time
This parameter is the nanoseconds that need to be added to the calculation time parameter to obtain the function result. For example3Second, you can write your code like this:
Dispatch_time_t delay =
Dispatch_time (dispatch_time_now, 3.0f * nsec_per_sec );Or the first half of the second:
Dispatch_time_t delay =
Dispatch_time (dispatch_time_now, (1.0/2.0f) * nsec_per_sec );
2. dispatch_after_f
Assign oneCFunctionGCDThis function is executed after a limited time in seconds.4Parameters:
Delay in nanoseconds
Before executing a given function(Specified by the fourth Parameter) GCDMust be in the specified delivery queue(Specified by the second parameter)The number of seconds to wait.
Dispatch queue
CFunction(Specified by the fourth Parameter) Must be at the given latency(Specified by the first parameter)Then the sub-delivery queue is executed on it.
Context
A value is passed toCFunction Memory Address(For example, see5.5).
CFunction
In a specific time period(Specified by the first parameter)Which must be executed later CFunction in the given dispatch queue(From the second
Parameter specified).
{
DoubleDelayinseconds = 2.0;
Dispatch_time_tDelayinnanoseconds =Dispatch_time(Dispatch_time_now, Delayinnanoseconds *Nsec_per_sec);
Dispatch_queue_tCur =Dispatch_get_global_queue(Dispatch_queue_priority_default, 0 );
Dispatch_after_f(Delayinnanoseconds, cur,Null,Processsomething);
}
VoidProcesssomething (Void* Paramcontext)
{
/* Do your processing here */
Nslog(@ "Processing ...");
}
2. Execute a task on GCD at most once-use the dispatch_once Function
FunctionDispatch_once, It accepts2Items
Parameters:
Token
A ClassDispatch_once_tOfToken HoldGCDThis is generated when the code block is executed for the first time.Token. If
If you want to execute a piece of code at most onceAppYou must specify
Specify the sameToken.
Block object
Block objectExecute at most once. ThisBlock objectNo value and no parameters are returned.Dispatch_onceCode that is always called is used to execute tasks on the current queue. It may be a serial queue, a concurrent queue, or a main queue column.
StaticDispatch_once_tOncetoken;
Dispatch_once(& Oncetoken, ^ {
});