First, Dispatch_after
Function: Delay the execution of a task in a queue for a period of time and cannot be canceled after it is returned
Often used to defer a task on the main queue
Function prototypes
[Plain] View PlainCopy
- Func Dispatch_after (_ when:dispatch_time_t,
- _ Queue:dispatch_queue_t!,
- _ block:dispatch_block_t!)
Parameters
[Plain] View PlainCopy
- How long after the time interval of execution
- Queues submitted to queue
- Tasks performed by block
For example, you can use Dispatch_after to write a delay function of your own, delay a period of time on the main thread to execute a piece of code
[Plain] View PlainCopy
- func hwcdelay (delay:double, closure: ()) {
- dispatch_after (
- dispatch_time (
- dispatch_time_now,
- int64 (Delay * double (nsec_per_sec))
- ),
- dispatch_get_main_queue (), closure)
- }
Only need to use this
Hwcdelay (0.5) {
Do everything want
}
For example, when the user's app does not meet some of the requirements of our app (for example, our app needs Bluetooth to open), and then when the app starts to test Bluetooth off, you should give the user a hint. After the view loading is completed, delay to the user a hint, you can also add some animation to this hint, it is more than the view in the load to complete the direct display prompt is much better.
Example
After viewload, delay 1s, prompting a alertview
[Plain] View PlainCopy
- Class viewcontroller:uiviewcontroller{
- Func Hwcdelay (delay:double, Closure: ()) {
- Dispatch_after (
- Dispatch_time (
- Dispatch_time_now,
- Int64 (Delay * Double (NSEC_PER_SEC))
- ),
- Dispatch_get_main_queue (), closure)
- }
- Override Func Viewdidload () {
- Super.viewdidload ()
- Hwcdelay (1.0) {
- var Alertview = Uialertview (title: "Dispatch_after", Message: "Message", Delegate:self,cancelbuttontitle: "OK")
- Alertview.show ()
- }
- }
- Override Func didreceivememorywarning () {
- Super.didreceivememorywarning ()
- }
- }
Second, dispatch_apply
Function: Commits a task to the queue multiple times, whether parallel execution or serial execution is determined by the queue itself. Note that dispatch_apply does not return immediately after execution, and is a synchronous call.
[Plain] View PlainCopy
- Func dispatch_apply (_ Iterations:uint,
- _ Queue:dispatch_queue_t!,
- _ Block: ((UInt), Void)!)
Parameters
[Plain] View PlainCopy
- Number of iterations executions
- Queues submitted to queue
- Tasks performed by block
So, when do you use this function? It is not difficult to see from its function that if we can commit unrelated loops to the background thread execution in parallel, and the efficiency of the cyclic task scheduling to the background execution, can offset the overhead of the queue scheduling itself, then the efficiency will be significantly improved.
Example
For example, I have an array that stores a series of objects, and when initialized, these objects call a function once to perform the relevant calculations. These calculations have no effect on each other. At this point, we can use the dispatch_apply to initialize with an asynchronous queue. This simplifies the situation here.
[Plain] View PlainCopy
- Class viewcontroller:uiviewcontroller{
- var hwcarray = ["Hello", "HWC", "HELLOHWC"]
- Override Func Viewdidload () {
- Super.viewdidload ()
- Dispatch_apply (3,dispatch_get_global_queue (dispatch_queue_priority_default,0)) {
- (Index:uint), () in
- var expobject = self.hwcarray[int (index)] as NSString
- NSLog ("%d", expobject.length)
- }
- NSLog ("Dispatch_after is Over")
- }
- Override Func didreceivememorywarning () {
- Super.didreceivememorywarning ()
- }
- }
As you can see, the output is
[Plain] View PlainCopy
- 3
- 5
- 8
- Dispatch_after is Over
Because this blocks the main thread, and the following is not relevant to the dispatch_apply execution results, you can drop dispatch_apply in the asynchronous queue and then notify when the execution is complete
[Plain] View PlainCopy
- Class viewcontroller:uiviewcontroller{
- var hwcarray = ["Hello", "HWC", "HELLOHWC"]
- Override Func Viewdidload () {
- Super.viewdidload ()
- Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default,0)) {
- Dispatch_apply (3,dispatch_get_global_queue (dispatch_queue_priority_default,0)) {
- (Index:uint), () in
- var expobject = self.hwcarray[int (index)] as NSString
- NSLog ("%d", expobject.length)
- }
- NSLog ("Dispatch_after in global queue are over")
- }
- NSLog ("Dispatch_after in main queue are over")
- }
- Override Func didreceivememorywarning () {
- Super.didreceivememorywarning ()
- }
- }
This output is
[Plain] View PlainCopy
- 8
- Dispatch_after in main queue are over
- 3
- 5
- Dispatch_after in global queue are over
As you can see, the Relative Home column (main thread) is asynchronous and is executed in parallel in the global queue
Third, dispatch_once
Function: ensure that the code in the block executes only once during the app run
Func Dispatch_once (_ Predicate:unsafemutablepointer<dispatch_once_t>,
_ block:dispatch_block_t!)
Parameters
predicate is used to determine if the committed block is completed
Block performs a task once
Dispatch_once's classic practical scenario is a single case
Single-Case Code:
[Plain] View PlainCopy
- Class Hwcsingleton {
- var testvariable:int!
- Func print () {
- testvariable = testvariable + 1
- println (testvariable)
- }
- class Var Sharedobject:hwcsingleton {
- struct Staticstruct {
- static var predicate:dispatch_once_t = 0
- static Var Instance:hwcsingleton? = Nil
- }
- Dispatch_once (&staticstruct.predicate) {
- Staticstruct.instance = Hwcsingleton ()
- Staticstruct.instance?. TestVariable = 10
- }
- Return staticstruct.instance!
- }
- }
Full details GCD Series (ii) dispatch_after;dispatch_apply;dispatch_once