The use of dispatch_apply
Simulating a For loop in parallel, looping the specified code 10 times, typically attaching the code to a queue and then parallel in the dispatch_apply
dispatch_queue_t queue = Dispatch_get_globel_queue (dispatch_queue_preority_default,0)
dispatch_apply, queue, ^ (size_t index) {
NSLog ("Parallel execution--%@", index);
});
The use of Dispatch_apply_f
Dispatch_apply and Dispatch_apply_f are synchronous functions that block the current thread until all loop iterations are completed. The order in which the loop iterations are executed is indeterminate when committed to a concurrent queue
Dispatch_after
Perform a task after a specified time is delayed
dispatch_after(dispatch_time_t when, dispatch_queue_t queue, ^(void)block)
when:延迟执行时间 一般是一个这样的时间函数: dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC))
Queue: Queues Dispatch_get_main_queue () or Dispatch_get_global_queue ()
Block: Tasks to perform
Dispatch_once
Dispatch_once (dispatch_once_t *predicate, ^ (Void) block)
Predicate: Used to determine whether the task to be performed is completed
Block: A one-time task that needs to be performed
+ (Instantclass *) sharedclient {
static Instantclass *_sharedclient = nil;
static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
_sharedclient = [[Instantclass alloc] init];
});
return _sharedclient;
}
Dispatch_group
Dispatch_grpup, put a number of tasks into a dispatch group
dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_preority_default,0);
Dispatch_queue_group group = Dispatch_group_craete ();
DISPATCH_GROUP_AYSNC (Group, queue,^{
Do some async work
});
Dispatch_group_async, like Dispatch_async, would put the task into a queue, except that it was a step more than Dispatch_async, which was to correlate the task with the group,
After putting these tasks into group, we can call dispatch_group_wait to wait for the task to complete. If the task is complete or empty, return directly, otherwise wait for all tasks to be completed. Notice that the task is emptied after returning.
Dispatch_group_wait (Group,dispatch_time_forver);
Dispatch_release (group);
The use of dispatch_apply