dispatch_async不能一直工作

时间:2013-11-14 18:57:19

标签: ios multithreading grand-central-dispatch dispatch-async

我有以下代码......

-(void) SetSerialNumber
{
    NSLog(@"SetSerialNumber");
    NSString *serialNum = textFieldSecond.text;
    if (textFieldSecond.text == nil) {
        [self performSelectorOnMainThread:@selector(display:) withObject:@"Please Enter the serial number" waitUntilDone:YES];
        return;
    }

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void){
        NSString* response;
        [self performSelectorOnMainThread:@selector(display:) withObject:@"Sending Set  Serial Num request" waitUntilDone:YES];
        [[testApp sharedClass] SetSerNumber:serialNum];

        [self performSelectorOnMainThread:@selector(display:) withObject:@"Waiting for Response..." waitUntilDone:YES];
        response = [[Process sharedProcess] readAndProcessData:ACK];
        [self performSelectorOnMainThread:@selector(display:) withObject:rpcresponse waitUntilDone:YES];
    });
}

方法display:将邮件置于UITextView

按下按钮时调用此功能。有时不调用dispatch_async块。它刚刚落空了。但是,我每次都可以看到日志消息。

任何人都可以分享可能导致这种情况的原因吗?

1 个答案:

答案 0 :(得分:2)

“跌倒”是什么意思?该调用是异步的,因此不会发生。将一个日志添加到块本身,看看它是否被打印。

你确定当它“落伍”时,serialNum不是零吗?