如何识别哪个Async调用

如何识别哪个Async调用

本文介绍了创建多个NSURLConnections。如何识别哪个Async调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用NSURLConnection创建2个请求。当服务器响应并调用connectionDidFinishLoading时,它在连接中作为参数传递,但是如何确定传递哪个连接?

I am intending to create 2 requests using NSURLConnection. When the server responds and calls connectionDidFinishLoading it passes in the connection as the parameter, but how do I identify which connection is passed in?

推荐答案

p>将NSURLConnection对象保存为传递给connectionWithRequest:delegate:的任何委托对象的成员变量。然后你可以只是比较每个这些NSURLConnection传递给connectionDidFinishLoading :,并适当响应:

Save both NSURLConnection objects as member variables of whatever delegate object you passed to connectionWithRequest:delegate:. Then you can just compare each of those to the NSURLConnection passed to connectionDidFinishLoading:, and respond appropriately:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (connection == firstConnection) {
        // do something
    }
    else if (connection == secondConnection) {
        // do something else
    }
}

是创建两个不同的委托对象,每个都知道如何处理每种类型的连接。然后在创建每个连接时传递适当的委托。这样,你不需要检查看看你有哪个连接,因为每个代理只会接收connectionDidFinishLoading:用于它自己的连接。

Another slightly more object-oriented option would be to create two different delegate objects, each of which knows how to deal with each each type of connection. Then just pass the appropriate delegate when you create each connection. That way you don't need to check to see which connection you have, because each delegate will only receive connectionDidFinishLoading: for its own connection.

这篇关于创建多个NSURLConnections。如何识别哪个Async调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:19