IOS Efficient development-----delay execution with GCD

Source: Internet
Author: User

In the early days we used the method of delay execution to provide the NSObject class, Performselector: The method of the series, specifically what we look at

We generally let an object delay execution of a method will call the method containing the parameter Afterdelay, this parameter represents the delay how long to execute, but the parameters of this series of methods only accept inherit from the NSObject class object, that is, if we want to pass the basic data type, That must involve data type conversion, which obviously increases the overhead, and this series of methods can be passed at most as a parameter, if we want to pass multiple parameters what to do, if we want to continue to use this method, then we have to put a number of parameters into an array or dictionary, and then the array or Dictionary object to this method, Then it will add us. Inserting an array or dictionary, parsing an array or dictionary of code, the amount of data to achieve a certain situation, the cost is conceivable, and we also need to know the array and dictionary each object represents what, very troublesome;

But we can use blocks to solve this problem, GCD provides us with a block function that demonstrates execution, which is defined as follows:

void dispatch_after ( dispatch_time_t when , dispatch_queue_t queue, dispatch_block_t block );

When we call this method, the system is also very thoughtful, when we write dispatch_after, this complete function will be presented, we look at

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{            });

The invocation is convenient, if we want to put inside the main thread to run, it is also very convenient, for example:

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{    Dispatch_async (Dispatch_get_main_queue (), ^{        [son study];    });

Still remember that there is a problem, is to give UIButton click event Add material, so that all the buttons in the system are forbidden to quickly click or hit, at that time asked to read a lot of blogs, there is no good solution, the previous article is the discussion or suggestions, are using performselector: Afterdelay this approach, but in that case, I'm going to implement another method. And then it's settled, here again, quoting what I wrote earlier, overriding the parent function, and then using GCD's Dispatch_after method to solve the problem;

The specific implementation is as follows:

  commonbutton.h//  commonbutton////  Created by PK on 14/12/24.//  Copyright (c) 2014 PK. All rights reserved.//#import <UIKit/UIKit.h> @interface commonbutton:uibutton@end

  

  commonbutton.m//  commonbutton////  Created by PK on 14/12/24.//  Copyright (c) 2014 PK. All rights reserved.//#import ' CommonButton.h ' @implementation commonbutton/*//only override drawrect:if you perform Cus Tom drawing.//An empty implementation adversely affects performance during animation.-(void) DrawRect: (cgrect) rect {
   
    //Drawing code}*/-(void) SendAction: (SEL) action to: (ID) Target forevent: (uievent *) event{    [Super SendAction: Action To:target Forevent:event];    self.enabled = NO;    Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{        self.enabled = YES;    });} @end
   

  

This realization, very clear, concise structure, simple to use, want to use this method, as long as the class will be changed to inherit the line;

Summary: Performselector Series method can handle the selector is too limited, the selector of the return value type and the number of parameters will be limited;

And Dispatch_after does not have these problems, in addition, if you want to put the task on another thread to execute, it is best not to use the Performselector series method, but the task should be encapsulated in the block, and then call GCD related methods to implement the line

IOS Efficient development-----delay execution with GCD

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.