GCD一次性代码

来源:互联网 发布:java考试及应用 编辑:程序博客网 时间:2024/05/16 18:43
@interface ViewController ()@property (nonatomic,assign) BOOL hasExecuted;@property (nonatomic,strong) ImageDownloader *imageDownloader;@end@implementation ViewController- (void)viewDidLoad {        [super viewDidLoad];    //    self.imageDownloader = [[ImageDownloader alloc] init];   }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {        /*    if (self.hasExecuted) {        return;    }        NSLog(@"下载图片");    self.hasExecuted = YES;     *///    ImageDownloader *imageDownloader = [[ImageDownloader alloc] init];//    [imageDownloader download];//    [self.imageDownloader download];       NSLog(@"touchBegin");    static dispatch_once_t onceToken;     // 保证只执行一次    dispatch_once(&onceToken, ^{        NSLog(@"once");    });        NSLog(@"touchEnd");        /** 点击两次     *  2015-08-10 17:26:23.219 10 - 一次性代码[5583:156692] touchBegin     2015-08-10 17:26:23.221 10 - 一次性代码[5583:156692] once     2015-08-10 17:26:23.221 10 - 一次性代码[5583:156692] touchEnd     2015-08-10 17:26:23.963 10 - 一次性代码[5583:156692] touchBegin     2015-08-10 17:26:23.963 10 - 一次性代码[5583:156692] touchEnd     */    }

0 0
原创粉丝点击