• 延迟调用-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
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    函数依赖(转)
    C++对象的深拷贝和浅拷贝
    C++临时匿名对象
    C++操作符重载
    C数组和指针
    动态开发入门之Servlet
    数据库的CURD操作 以及经典的sql语句
    如何删除07版word页眉页脚的横线
    数据库的连接查询
    经典面试题 详细解析Java中抽象类和接口的区别
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5416812.html
Copyright © 2020-2023  润新知