在执行下一条语句之前,如何等待 NSURLConnection 委托完成?

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

本文介绍了在执行下一条语句之前,如何等待 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 脚本,该脚本返回一个带有配置设置的 JSON 文档(包括 rtmp 转储链接).

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,然后在委托中继续设置代码inside方法 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?

推荐答案

我已经意识到我可以像现在一样在 -recordButtonPressed 中创建 NSURLConnection,然后在委托方法 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 {
}

您可以通过调用玩各种游戏,以使其更加优雅和通用,但我的建议是,不要与框架抗争,因为您所描述的正是异步的本质.(并且不要在主线程上使用同步请求,因为这会阻塞主线程,这是不行的.)

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 委托完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在执行下一条语句之前,如何等待 NSURLConnection 委托完成?