@selector如何影响iOS中的程序流?

时间:2014-09-25 12:57:26

标签: ios objective-c iphone

我是iOS编程的新手,现在我编写了一个必须连接到SOAP WS的App。我使用来自Internet的软件(SudzC)来为WS生成包络类。要从服务接收响应,它使用@selector将执行传递给响应处理程序例程。我认为,当程序流程转到响应的处理程序例程时,它将返回到初始例程(在完成其工作之后),但令我惊讶的是,我发现调用例程已完成并且在调用该处理程序例程之后。可能是我不明白iOS程序流程正确!

是否有涉及@selector的程序流程的下降解释?

修改 以下是示例代码:

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

    WS* service = [WS service];
    service.logging = YES;

    [service WS_Method:self action:@selector(WS_MethodHandler:) iParam1: 1 sParam2: @"string" iParam3: 1];

    NSLog(@"!!! Finish !!!");
}

- (void) WS_MethodHandler: (id) value {

    // Handle errors
    if([value isKindOfClass:[NSError class]]) {
        NSLog(@"%@", value);
        return;
    }

    // Handle faults
    if([value isKindOfClass:[SoapFault class]]) {
        NSLog(@"%@", value);
        return;
    }

    WS_CheckIussue* result = (WS_CheckIussue*)value;
    NSLog(@"WS_CheckIussue returned the value: %@", result);
 NSLog(@"!!! Finish !!! 2");
}

结果我有!!!完成!!!在那之后 !!!完成!!! 2

1 个答案:

答案 0 :(得分:0)

网络代码通常在单独的线程上运行。所以我假设您的代码看起来像这样:

- (void)downloadData
{
    //Step 1 : Request data from webservice
    WebServiceDohickey * webservice = [[WebServiceDohickey alloc] init];
    //This will run on a separate thread. 
    //The selector passed in may be performed during or after any other code in this method
    [webservice getWebServiceResponseWithCallback:@selector(responseHandler:)
                                           target:self];

    //Step 3 : Invalid program flow area
    //I'm guessing you have more code here that you expect to happen after the code in your responseHandler, but it actually executes before. That code should be in the responseHandler method
}
- (void)responseHandler:(WebServiceResponse *)response
{
    //Step 2 : Handle response object

    //Step 3 : This is where step 3 should live

    //Side note : Your callback may need to specifically be put on a specific thread.
    //Wrap step 2 and 3 in a block like this to put it on the main thread which is common for UI code execution.
    dispatch_async(dispatch_get_main_queue(), ^{

    });
}