Simple summary of various types of delay triggering

Source: Internet
Author: User

The Performselector method is typically used to delay operations in a specified method, or to pass parameters in a specified method. However, the Performselector method is lack of security in the delay operation, in the case of high security requirements, can call Dispatch_after to achieve a single time delay call.

(1) Dispatch_after achieve 3 second delay:

1 3*nsec_per_sec); 2     Dispatch_after (Time, Dispatch_get_main_queue (), ^{3        [self dismisspage]; 4     }); 5-(void) dismisspage{6     NSLog (@ " three seconds away ");     [Adview Removefromsuperview]; 8}

(2) Some simple summaries of the Performselector method:

Note: The default mode for Performselector is: Nsdefaultrunloopmode. Runloop is a message processing mechanism in which a thread can perform only one task at a time, and the thread exits after execution completes. If we need a mechanism that allows threads to handle events at any time without exiting, Runloop is the mechanism for handling such tasks.

1     //1. Normal method Invocation2 [Self performselector: @selector (dismisspage)];3     //2. Method invocation of the parameter transfer4NSString *str =@"mo Xue";5    //[Self performselector: @selector (dismisspage:) withobject:str];6     //3. Delay parameter7[Self performselector: @selector (dismisspage:) withobject:str Afterdelay:3.0];8     //4. Execute the specified method on the main thread, using the default mode (Nsdefaultrunloopmode). 9     //The default mode refers to: The method in the main thread is queued, is a loop queue, and is executed in a loop. Ten     //Wait: The Aselector method to execute is executed immediately.  One     //If set to Yes: Wait for the current thread to finish executing, the main thread will not execute the Aselector method; A     //set to No: Do not wait for the current thread to finish executing the Aselector method on the main thread.  -     //if the current thread is the main path, the Aselector method executes immediately.  - [Self performselectoronmainthread: @selector (dismisspage:) withobject:str Waituntildone:yes]; the  -  --(void) Dismisspage: (NSString *) ad{ -NSLog (@"%@", AD); + [Adview Removefromsuperview]; -      } +-(void) dismisspage{ ANSLog (@"three seconds to disappear ."); at [Adview Removefromsuperview]; -}

(3) Nstimer implementation delay:

1     //the first method of initializing the Nstimer:2Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:3target:self selector: @selector (dismisspage) Userinfo:nil Repeats:nil];3     //the second method of initializing the Nstimer:4Nstimer *timer = [Nstimer timerwithtimeinterval:3.0target:self selector: @selector (dismisspage) Userinfo:nil Repeats:no];5 [[Nsrunloop Currentrunloop]addtimer:timer Formode:nsdefaultrunloopmode];6 7-(void) dismisspage{8NSLog (@"three seconds to disappear .");9 [Adview Removefromsuperview];Ten}

Trigger or start of Nstimer:

You can use the-(void) Fire method to trigger the timer immediately;

Stop of Nstimer:

-(void) invalidate; Remove the timer from the Runloop.

Simple summary of various types of delay triggering

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.