//
//  ViewController.m
//  test_runloop_03
//
//  Created by cdd on 16/8/2.
//  Copyright © 2016年 jeffasd. All rights reserved.
//

#import "ViewController.h"

#define TICK   NSDate *startTime = [NSDate date]
#define TOCK   NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    TICK;
    
    NSLog(@" get New 111");
    __block BOOL isGetNewUserSourceData = NO;
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        isGetNewUserSourceData = YES;
    });
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        
//        
//        dispatch_sync(dispatch_get_main_queue(), ^{
//            
//            isGetNewUserSourceData = YES;
//        });
//        
//    });
    
    while (isGetNewUserSourceData == NO) {
        
//        NSLog(@"runloop is %@", [NSRunLoop currentRunLoop]);
        
        static int i = 0;
        i++;
        NSLog(@"i is %d", i);
        
        //手动修改主线程的运行模式可能导致主线程的 所用事件都是激活状态 导致其进入死循环状态
//        [[NSRunLoop currentRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate distantFuture]];
//        [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
//        [[NSRunLoop currentRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate distantFuture]];
        //主线程最好不要修改runloop的运行模式 主线程的运行模式让主线程自己切换
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];
    }
    
    TOCK;
    NSLog(@" get New ");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end