Several ways to delay execution of the iOS development series

Source: Internet
Author: User

Tag:ios    delay execution    

/* This article lists four ways to delay the execution of a function and some of its 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 execution mode,//the method for canceling execution is not found temporarily. /** *  "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 way of execution,//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 a blocking method of execution, which is placed in a sub-thread to avoid jamming the interface//not finding a way 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];}); /This way you can select the executing thread in the parameter. is a non-blocking way of execution,//the method of canceling execution is not found.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Several ways to delay execution of the iOS development series

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.