Summary of common delay methods
This paper lists four methods of delaying execution of a function and some differences. If the delay is 1 seconds, execute the following method.
-(void) Delaymethod {NSLog (@ "execute");}
1.performSelector Method:(There is no such method in Swift because this method is considered unsafe in swift)
[Self performselector: @selector (Delaymethod) withobject:nil afterdelay:1.0f];
This method requires that it must be executed in the main thread, otherwise it is not valid.
is a non-blocking method of execution,
The method for canceling execution is not found yet.
2. Timer: Nstimer
1 [nstimer scheduledtimerwithtimeinterval:1.0f target:self selector: @selector (Delaymethod) Userinfo:nil Repeats:no];
This method requires that it must be executed in the main thread, otherwise it is not valid.
is a non-blocking method of execution,
The Nstimer class can be invalidate by the-(void) or cancel execution.
3. Sleep mode
[Nsthread sleepfortimeinterval:1.0f]; [Self delaymethod];
This method can be executed in both the main thread and the child thread.
is a blocking method of execution that is placed in a sub-thread to avoid jamming the interface
No method was found to cancel execution.
4.GCD Way
1 double delayinseconds = 1.0 ; 2 3 __block viewcontroller* bself = self; 4 5 dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, (int64_t) (delayinseconds * nsec_per_sec)); 6 void 8 9 [bself Delaymethod]; });
In this way, the executing thread can be selected in the parameter.
is a non-blocking method of execution,
No method was found to cancel execution.
How to implement in Swift
1.sleep
1 //Create a destination queue2Let Workingqueue = Dispatch_queue_create ("My_queue", nil)3 4 //Dispatched to the queue you just created, GCD is responsible for thread scheduling5 Dispatch_async (workingqueue) {6 //asynchronously in the Workingqueue7println"work hard")8Nsthread.sleepfortimeinterval (2)//simulate two seconds of execution time9 Ten Dispatch_async (Dispatch_get_main_queue ()) { One //back to the main thread update UI Aprintln"end work, update UI") - } -}
2.GCD
1 2.0 2 Let delay =3 Int64 (Time * Double (nsec_per_sec)))4Dispatch_ After (delay, dispatch_get_main_queue ()) {5 println ("output after 2 seconds " )6 }
Wrap it up.
1 Import Foundation2 3Typealias Task = (cancel:bool) ()4 5Func Delay (Time:nstimeinterval, Task: ()), task? {6 7Func Dispatch_later (Block: ()()) {8 Dispatch_after (9 Dispatch_time (Ten Dispatch_time_now, OneInt64 (Time *Double (NSEC_PER_SEC))), A dispatch_get_main_queue (), - block) - } the -var closure:dispatch_block_t? =Task -var result:task? - +Let Delayedclosure:task = { -Cancelinch + ifLet internalclosure =Closure { A if(Cancel = =false) { at Dispatch_async (Dispatch_get_main_queue (), internalclosure); - } - } -Closure =Nil -result =Nil - } in -result =delayedclosure to + Dispatch_later { - ifLet delayedclosure =Result { theDelayedclosure (Cancel:false) * } $ }Panax Notoginseng - returnresult; the } + AFunc Cancel (task:task?) { theTask? (Cancel:true) +}
Use:
let task = delay(5) { println("拨打 110") }// 仔细想一想..// 还是取消为妙..cancel(task)
iOS Development-a summary of practical techniques & common delay methods (Oc/swift)