Comparison and summarization of several ways of delaying execution in iOS

Source: Internet
Author: User



Reprinted from:

Http://blog.sina.com.cn/s/blog_8280f5ec0101k03c.html

http://www.jianshu.com/p/21d351116587

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 it a non-blocking way 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 it a non-blocking way of execution that can be invalidate by Nstimer class-(void);
3. Sleep mode

[Nsthread sleepfortimeinterval:1.0f]; [Self delaymethod];


This method can be executed in both the main thread and the child thread. Is it a kind of blocking execution that is put in a sub-thread to avoid sticking to 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));d Ispatch_after ( Poptime, Dispatch_get_main_queue (), ^ (void) {[bself delaymethod];});


In this way, you can select the thread of execution in the parameter. Is it a non-blocking way of execution? No method was found to cancel execution.

Comparison and summarization of several ways of delaying execution in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.