利用GCD实现延迟(实现延迟的几种方法)

来源:互联网 发布:淘宝衣服上的胸围是指 编辑:程序博客网 时间:2024/05/01 10:10


    // 在主线程中延迟执行某动作,不会卡主主线程,不影响后面的东做执行

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSLog(@"%@", [NSThread currentThread]);

    });


    // 在子线程中执行某动作,不会卡主整个线程

    dispatch_queue_t queue  = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{


        NSLog(@"%@", [NSThread currentThread]);

    });



    // 实现延迟,该线程本身在哪个线程中就再哪个线程中执行

    NSURL *url = [NSURL URLWithString:@"http://59320.jpg.com"];

    [self performSelector:@selector(download:) withObject:url afterDelay:3];



    // 利用sleep实现延迟(不要用这个,会卡住主线程,即后面的动作不会执行)

    [NSThread sleepForTimeInterval:3];


0 0
原创粉丝点击