Wait for code to finish execution

前端 未结 2 1985
孤街浪徒
孤街浪徒 2020-12-17 01:39

I would like to know the easiest way to wait for code to finish execution within an objective c project because I am calling a webservice and retrieving the results and inst

相关标签:
2条回答
  • 2020-12-17 02:09

    You could use one of many of the Cocoa design patterns (Delegate, Notification, etc).

    For instance you would trigger the method and wait until you receive a response back.

    It looks like you are using an asynchronous request and for this case, you would need to wait until one of the delegate methods get notified that the request has finished (with error or success).

    BTW, what does your request look like? Could you share some code to explain how you do the request and when and what you want to do?

    Edited after the code was inserted:

    You set self as the delegate of the request, and so you should be able to handle the responses.

    Have a look at the NSURLConnection Class Reference. You will need to trigger your parser when the request finishes on these methos, for example:

    – connection:didReceiveResponse:
    – connection:didReceiveData:
    – connection:didFailWithError:
    

    Cheers,

    vfn

    0 讨论(0)
  • 2020-12-17 02:15

    You do it easily as below,

    -(void)aFunc {
    
    Do Asynchronous A job...
    
    while (A is not finished) {
    // If A job is finished, a flag should be set. and the flag can be a exit condition of this while loop
    
    // This executes another run loop.
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    
    }
    
    Do things using the A's result.
    
    }
    
    0 讨论(0)
提交回复
热议问题