4 ways to compare and analyze IOS delayed execution _ios

Source: Internet
Author: User
Tags gcd

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.

Related Article

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.