延迟调用-05-GCD

 1 //
 2 //  ViewController.m
 3 //  05-GCD延迟调用
 4 //
 5 //  Created by mac on 16/4/21.
 6 //  Copyright © 2016年 mac. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()
12 
13 @end
14 
15 @implementation ViewController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     
20 }
21 
22 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
23     
24     [self delay3];
25 }
26 
27 /**
28  *  非延迟调用
29  */
30 - (void)delayThread1 {
31  
32     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
33     dispatch_after(DISPATCH_TIME_NOW, queue, ^{
34        
35         NSLog(@"delay === %@", [NSThread currentThread]);
36     });
37 }
38 /**
39  *  延迟调用,且开启多线程
40  */
41 - (void)delayThread2 {
42     
43     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
44     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
45         
46         NSLog(@"delayThread2===%@", [NSThread currentThread]);
47     });
48 }
49 
50 /**
51  *  一下三个方法实现了延迟调用,未开启多线程,在主线程中执行
52  */
53 - (void)delay1 {
54     
55     [NSThread sleepForTimeInterval:3];
56     NSLog(@"delay1=%@", [NSThread currentThread]);
57 }
58 - (void)delay2 {
59     
60     [self performSelector:@selector(download:) withObject:@"http://333.jpg" afterDelay:3];
61 }
62 - (void)delay3 {
63     
64     dispatch_queue_t queue = dispatch_get_main_queue();
65     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
66         
67         NSLog(@"delay3=%@", [NSThread currentThread]);
68         
69     });
70     
71     /*GCD :与上面的等价
72      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
73         
74         NSLog(@"delay3=%@", [NSThread currentThread]);
75         
76     });*/
77     
78     
79     
80      NSLog(@"delay====%@", [NSThread currentThread]);
81 }
82 
83 - (void)download:(NSString *)url {
84     
85     NSLog(@"delay2=%@", [NSThread currentThread]);
86 }
87 
88 
89 @end

 

posted on 2016-04-21 14:30  爱你久久iOS  阅读(157)  评论(0编辑  收藏  举报

导航