异步Web服务调用NSURLConnection的代理的NSURLRequest [英] NSURLConnection NSURLRequest proxy for asynchronous web service calls

查看:233
本文介绍了异步Web服务调用NSURLConnection的代理的NSURLRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有做出同样的的NSURLRequest / NSURLConnection的要求多个视图。理想的情况下,为了得到一些code重用,我想有一些这确实创建/执行(异步)请求/连接,设置所有委托方法的所有底层的工作,代理的等等,所以我没有所有那些 NSURLConnection的委托方法处理复制每个视图。首先,是这种设计方式合理吗?第二,我怎么会去这样做类似的东西?

I have multiple views which make the same NSURLRequest/NSURLConnection request. Ideally, in order to get some code reuse, I'd like to have some sort of a "proxy" which does all the underlying work of creating/executing the (asynchronous) request/connection, setting up all the delegate methods, etc., so I don't have to copy all those NSURLConnection delegate method handlers in each view. First of all, is this design approach reasonable? Second, how would I go about doing something like that?

有关的一些背景信息,我尝试这一点,得到了它的工作,但是,它似乎并不被异步执行。我创建了具有不同的Web服务调用实例方法(也包含 NSURLConnection的委托方法)一Proxy.h / M文件:

For a little background info, I attempted this and got it to "work", however, it doesn't appear to be executing asynchronously. I created a Proxy.h/m file which has instance methods for the different web service calls (and also contains the NSURLConnection delegate methods):

@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

该WSResponseProtocol的定义是这样的:

The WSResponseProtocol is defined as such:

@protocol WSResponseProtocol <NSObject>

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

@end

要使用此功能,视图控制器只需要符合 WSResponseProtocol 的协议,赶响应(S)。使Web服务调用,像这样完成的:

To use this, the view controller simply needs to conform to the WSResponseProtocol protocol, to catch the response(s). Making the web service call is done like so:

Proxy *p = [[Proxy alloc] init];
[p searchForSomethingAsync:searchText delegate:self];
[p release];

我可以提供更多的code,但剩余的可假设。打电话之前,我startAnimating一个 UIActivityIndi​​catorView 微调。但是,微调绝不旋转。如果我只是把NSURLConnection的委托方法直接在视图控制器,然后微调旋转。因此,这让我觉得我的实现不是异步执行。任何想法/这里的想法?

I can provide more code but the remaining can be assumed. Before calling, I "startAnimating" a UIActivityIndicatorView spinner. But the spinner never spins. If I simply put the NSURLConnection delegate methods directly in the view controller, then the spinner spins. So, it makes me think that my implementation isn't executing asynchronously. Any thoughts/ideas here?

推荐答案

您的做法是合理的,但是,我不知道你为什么要创建自己的协议。这是没有必要的。一切你需要得到这个实现的是在苹果的<一个href=\"http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html\">documentation在NSURLConnection的的。如果你把从该页面,其中NSURLConnection的实例化的code,并进行连接伊娃而不是仅仅创造它作为一个局部变量,就可以在每个回调方法比较连接对象,并作出相应的反应。例如,以从该文档code和更改连接对象伊娃:

Your approach is reasonable, however, I'm not sure why you are creating your own protocol. This is not necessary. Everything you need to get this implemented is in Apple's documentation on NSURLConnection. If you take the code from that page where the NSURLConnection is instantiated, and make the connection an ivar instead of just creating it as a local variable, you can then compare connection objects in each of the callback methods and respond accordingly. For example, take this code from the docs and change the connection object to an 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
}

变量的 theConnection 的是我们的伊娃。然后,你可以检查它是这样的:

The variable theConnection is our ivar. Then you can check it like this:

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

您肯定能实现它创建自己的协议,你是在暗示然后再打到符合您的协议委托,但你可能会更好只是实例用成功和失败的选择,你可以在对象检查。事情是这样的:

You can certainly implement it creating your own protocol as you're suggesting and then call back out to the delegate that conforms to your protocol, but you may be better off just instantiating your object using a success and failure selector that you can check. Something like this:

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

在哪里dataDidDownloadSelector是您在创建,所有这code的包含您的下载委托设置一个SEL实例变量 - 您的代理对象。事情是这样的:

Where dataDidDownloadSelector is a SEL instance variable that you set when you created your download delegate where all of this code is contained--your Proxy object. Something like this:

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

实施你的选择是这样的:

Implement your selectors like this:

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

- (void)didFailWithError:(NSError*)error
{
    // Do something with error
}

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

This has become a longer answer than I intended. Let me know if it doesn't make sense and I can try to clarify.

最好的问候,

这篇关于异步Web服务调用NSURLConnection的代理的NSURLRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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