Recently learned several methods of delaying execution, sharing:
1.performSelector (NSObject) method 2.NSTimer method 3.GCD method 4.sleep (Nsthread) method
First, the Performselector method:
Copy Code code as follows:
[Self performselector: @selector (Delaymethod) Withobject:nil afterdelay:1.0f];
1. Features: This method requires that it must be executed in the main thread, otherwise it is invalid. is a non-blocking way to execute, and the method for canceling execution is temporarily not found.
Second, timer: Nstimer:
Copy Code code as follows:
[Nstimer scheduledtimerwithtimeinterval:1.0f target:self selector: @selector (Delaymethod) Userinfo:nil Repeats:NO];
1. Features: This method requires that it must be executed in the main thread, otherwise it is invalid. is a non-blocking execution method that can be invalidate by Nstimer class-(void);
Third, sleep mode:
Copy Code code as follows:
[Nsthread sleepfortimeinterval:1.0f];
1. Features: This method can be performed both in the main thread and in child threads. is a blocking execution, it is recommended to put it in a child thread so that the card interface does not find a way to cancel the execution.
Four, GCD way:
Copy Code code as follows:
Dispatch_after (Dispatch_time (Dispatch_time_now, int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{ Dispatch_async (Dispatch_get_main_queue (), ^{[self delaymethod];}); });
1. Features: This way you can select the executing thread in the parameter. is a non-blocking way of executing, and no method of canceling execution is found.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.