在执行下一个语句之前,如何等待NSURLConnection委托完成? [英] How can I wait for a NSURLConnection delegate to finish before executing the next statement?

查看:154
本文介绍了在执行下一个语句之前,如何等待NSURLConnection委托完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很难搜索的内容。
我发现一个类似的问题, iOS 5等待代理完成之前填充表?,但接受的答案是刷新表视图,这不能帮助我。我发现其他结果往往在c#中。

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 table view,' and that does not help me. The other results I found tended to be in c#.

我有一个从iPhone流向Wowza服务器的应用程序。当用户命中记录时,我生成唯一的设备ID,然后将其发送到服务器上的PHP脚本,该脚本返回具有配置设置(包括rtmp转储链接)的JSON文档。

I have an app that streams from iPhone to Wowza servers. When the user hits record, I generate a unique device id, then send it to a PHP script on the server that returns a JSON document with configuration settings (which includes the rtmp dump link).

问题是,委托方法是异步的,但是我需要在我的 - (IBAction)recordButtonPressed 方法中的下一行代码之前获取配置设置,因为该代码是设置配置文件设置,然后根据这些设置进行记录。

The problem is, the delegate methods are asynchronous, but I need to get the config settings before the next lines of code in my - (IBAction)recordButtonPressed method, since that code is what sets the profile settings, and then records based on those settings.

我意识到我可以在 -recordButtonPressed NSURLConnection $ c>像我目前一样,然后继续执行代码 代理方法 connectionDidFinishLoading (或者只是封装了安装和方法调用它那里),但这是牺牲一致的设计的功能和吸引力。

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.

没有一些简单的 waitUntilDelegateIsFinished :( BOOL)nonAsyncFlag 标志我可以发送给委托人,所以我可以从网络中提取数据的顺序操作?

Is there not some simple waitUntilDelegateIsFinished:(BOOL)nonAsyncFlag flag I can send to the delegator so I can have sequential operations that pull data from the web?

推荐答案


我意识到我可以使NSURLConnection在-recordButtonPress像我目前一样,然后继续在委托方法connectionDidFinishLoading(或只是封装的安装和方法调用它从那里)的设置代码,但这是牺牲一致的设计的功能和吸引人。

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 {
}

您可以通过调用来玩各种游戏,使其更加优雅和普遍,但我的建议是,不要打框架,因为您正在描述的正是异步的本质。 (而且在主线程上使用同步请求,因为阻塞主线程,这是一个no-no。)

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.

这篇关于在执行下一个语句之前,如何等待NSURLConnection委托完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆