dispatch_async和performSelector

This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly. One common context where you might call this method and end up registering with a runloop that is not automatically run on a regular basis is when being invoked by a dispatch queue. If you need this type of functionality when running on a dispatch queue, you should use dispatch_after and related methods to get the behavior you want.



- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //1.直接调用会阻塞主线程
//    [self handelData];
//    //记录登录时间
//    [AccountInfo sharedAccount].LoginDate=[NSDate date];
//    [self.sideBarController setShowSideView:YES];
    
    
    
    //2.通过performSelector调用
//    [self performSelector:@selector(handleData) withObject:nil afterDelay:0.1];
    
    
    //3.GCD
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

<code class="java comments">        //这个selector不会执行</code><div class="line number22 index21 alt1"><code class="java spaces">        //</code><code class="java plain">[self performSelector:</code><code class="java color1">@selector</code><code class="java plain">(testDelay) withObject:nil afterDelay:</code><code class="java value">3</code><code class="java plain">];</code></div>        [self dd];
        dispatch_async(dispatch_get_main_queue(), ^{
            //记录登录时间
            [AccountInfo sharedAccount].LoginDate=[NSDate date];
            [self.sideBarController setShowSideView:YES];
        });
    });
    

}

- (void)dd{
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC);
    dispatch_after(time, dispatch_get_main_queue(), ^{
        [self <code>handelData</code>];
    });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值