IOS deferred execution method detailed _ios

Source: Internet
Author: User
Tags gcd sleep

Recently learned several methods of delaying execution, share.

1.performSelector (NSObject) method

2.NSTimer method

3.GCD method

4.sleep (Nsthread) method

Deferred code execution:

-(void) Delaydo: (ID) Sender {
  NSLog (@ "do:%@", sender);
}

1.performSelector (NSObject) method

This is a commonly used method of delaying execution in iOS.

Feature: This method must be used in the main thread. You can pass parameters. You can cancel the operation and you cannot pause it.

With no parameters
[self performselector: @selector (delaydo:) Withobject:nil afterdelay:1.0f];
with parameters
[self performselector: @selector (delaydo:) withobject:@ "abc" afterdelay:1.0f];

The cancel operation is divided into 2 kinds:
(1) Cancel all deferred execution operations
[NSObject cancelpreviousperformrequestswithtarget:self];
(2) Cancel the specified deferred execution action
When you cancel the specified delay operation, the only indication of the operation is the parameters passed, and only the correct parameters are passed to cancel the

Cancels the method of not passing the parameter
[nsobject cancelpreviousperformrequestswithtarget:self selector: @selector (delaydo:) Object:nil];
Methods for canceling the argument
[nsobject cancelpreviousperformrequestswithtarget:self selector: @selector (delaydo:) object:@ "abc"];
 

2.NSTimer method

Delay Method with Timer

Feature: This method must be used in the main thread. You can pass parameters. You can cancel an operation, you can pause it, you can perform a deferred operation immediately.

Because this method can be paused, a variable isrun is set here to determine whether it is running.

Timer Object
Nstimer * timer; 
Variable BOOL Isrun as the judge state of the timer
; 

Start the timer, Repeats:no. Only one execution at a time. YES, repeat execution.

Copy Code code as follows:
Isrun = YES;
Timer = [Nstimer scheduledtimerwithtimeinterval:1.0f target:self selector: @selector (delaydo:) userinfo:@ "abc" Repeats : YES];

Pause Operation:

if (isrun) {
#warning This method is paused the function is actually to pull the time far away
    isrun = NO;
    [Timer setfiredate:[nsdate distantfuture]];
  }
  else if (!isrun) {
#warning recovery timer
    isrun = YES;
    [Timer setfiredate:[nsdate Date]];
  }

Do not wait for timer, perform deferred operation immediately

[Timer fire];

Destroy/completely cancel timer

#waring This method to cancel the timer is not suspended
[Timer invalidate];

To illustrate here, not only the Invalidate method to destroy the timer, when the repeats property is set to No, the timer will be automatically destroyed after running.

What about the parameters? The Nstimer parameter is userinfo, so the UserInfo method is used to extract the correct argument

NSlog (@ "sender:%@", [sender UserInfo]);

3.GCD method

Features: This method does not restrict the thread, it is not easy to cancel the operation.

Why can't I cancel the operation? The code was given to GCD for automatic processing, and the developer was not easy to operate.

In the main thread delay execution  
 dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3 * nsec_per_sec)), Dispatch_get_main_ Queue (), ^{
   [self delaydo:@ "GCD"];
Deferred execution of
   Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5 * nsec_per_sec)) in child threads, Dispatch_get_global_ Queue (Dispatch_queue_priority_default, 0), ^{
   [self delaydo:@ "GLOBAL-GCD"];
   

Changing the number of (5 * nsec_per_sec) can change the delay time in seconds.

By default in the main thread, changing Dispatch_get_main_queue ()------->dispath_get_global_queue () allows code to execute on child threads.

4.Sleep (Nsthread) method

Features: card Master Current thread to implement deferred operation, use caution. Sometimes, it is very convenient to use.

[Nsthread Sleepfortimeinterval:3];

It is best not to use it in the main thread, otherwise it will be stuck to the interface.

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.