用于异步Web服务调用的NSURLConnection NSURLRequest代理

tbe*_*nin 5 iphone asynchronous uiviewcontroller nsurlconnection nsurlrequest

我有多个视图相同NSURLRequest/NSURLConnection request.理想情况下,为了获得一些代码重用,我想要某种"代理"来完成创建/执行(异步)请求/连接,设置所有委托方法等所有基础工作. ,所以我不必NSURLConnection在每个视图中复制所有委托方法处理程序.首先,这种设计方法是否合理?第二,我将如何做这样的事情?

对于一些背景信息,我尝试了这个并让它"工作",但是,它似乎不是异步执行.我创建了一个Proxy.h/m文件,其中包含不同Web服务调用的实例方法(还包含NSURLConnection委托方法):

@interface Proxy : NSObject {

    NSMutableData *responseData;
    id<WSResponseProtocol> delegate;
}

- (void)searchForSomethingAsync:(NSString *)searchString delegate:(id<WSResponseProtocol>)delegateObj;

@property (nonatomic, retain) NSMutableData *responseData;
@property (assign) id<WSResponseProtocol> delegate;

@end
Run Code Online (Sandbox Code Playgroud)

WSResponseProtocol定义如下:

@protocol WSResponseProtocol <NSObject>

@optional
- (void)responseData:(NSData *)data;
- (void)didFailWithError:(NSError *)error;

@end
Run Code Online (Sandbox Code Playgroud)

要使用它,视图控制器只需要符合WSResponseProtocol协议,以捕获响应.进行Web服务调用是这样完成的:

Proxy *p = [[Proxy alloc] init];
[p searchForSomethingAsync:searchText delegate:self];
[p release];
Run Code Online (Sandbox Code Playgroud)

我可以提供更多代码,但剩下的可以假设.在打电话之前,我"开始动画"一个UIActivityIndicatorView微调器.但旋转器从不旋转.如果我只是将NSURLConnection委托方法直接放在视图控制器中,那么微调器就会旋转.所以,它让我觉得我的实现不是异步执行的.这里有什么想法/想法吗?

Mat*_*ong 22

你的方法是合理的,但是,我不确定你为什么要创建自己的协议.这不是必需的.实现这一目标所需的一切都在Apple的NSURLConnection文档中.如果您从实例化NSURLConnection的页面获取代码,并使连接成为ivar而不是仅将其创建为局部变量,则可以比较每个回调方法中的连接对象并进行相应的响应.例如,从文档中获取此代码并将连接对象更改为ivar:

// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                    timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}
Run Code Online (Sandbox Code Playgroud)

变量theConnection是我们的ivar.然后你可以这样检查:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (connection == theConnection)
    {
        // do something with the data object.
        [connectionSpecificDataObject appendData:data];
    }
}
Run Code Online (Sandbox Code Playgroud)

您当然可以按照建议的方式实现它创建自己的协议,然后回调给符合您协议的委托,但最好只使用您可以检查的成功和失败选择器来实例化您的对象.像这样的东西:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == theConnection)
    {
        if (delegate && [delegate respondsToSelector:successSelector])
            [delegate performSelector:successSelector 
                           withObject:connectionSpecificDataObject];
    }
    [connection release];
}
Run Code Online (Sandbox Code Playgroud)

其中dataDidDownloadSelector是您在创建包含所有此代码的下载委托时设置的SEL实例变量 - 您的Proxy对象.像这样的东西:

Proxy *p = [[Proxy alloc] init];
[p searchForSomethingAsync:searchText 
                  delegate:self 
           successSelector:@selector(didFinishWithData:) 
              failSelector:@selector(didFailWithError:)];
Run Code Online (Sandbox Code Playgroud)

像这样实现您的选择器:

- (void)didFinishWithData:(NSData*)data;
{
    // Do something with data
}

- (void)didFailWithError:(NSError*)error
{
    // Do something with error
}
Run Code Online (Sandbox Code Playgroud)

这已成为比我预期的更长的答案.如果它没有意义,让我知道,我可以尝试澄清.

最好的祝福,