comparison and summarization of several ways of delaying execution in iOS
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
[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
[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 mode
Double delayinseconds = 1.0;
__block viewcontroller* bself = self;
dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, (int64_t) (Delayinseconds * nsec_per_sec));
Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {
[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.
Comparison and summarization of several ways of delaying execution in iOS