How can I wait for a NSURLConnection delegate to finish before executing the next statement?

前端 未结 3 905
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 19:31

This has been a hard one to search. I found a similar question, iOS 5 Wait for delegate to finish before populating a table?, but the accepted answer was \'Refresh the tabl

3条回答
  •  太阳男子
    2021-01-15 19:46

    I've realized I could make the NSURLConnection in -recordButtonPressed like I am currently, and then continue the setup code inside the delegate method connectionDidFinishLoading (or just encapsulate the setup and method call it from there) but that's sacrificing coherent design for functionality and that sucks.

    You have analyzed and understood the situation and you have described its possible solutions perfectly. I just don't agree with your conclusions. This kind of thing happens all the time:

    - (void) doPart1 {
        // do something here that will eventually cause part2 to be called
    }
    
    - (void) doPart2 {
    }
    

    You can play various games with invocations to make this more elegant and universal, but my advice would be, don't fight the framework, as what you're describing is exactly the nature of being asynchronous. (And do not use a synchronous request on the main thread, since that blocks the main thread, which is a no-no.)

    Indeed, in an event-driven framework, the very notion "wait until" is anathema.

提交回复
热议问题