English 中文(简体)
ASIHTTPRequest code design
原标题:

I m using ASIHTTPRequest to communicate with the server asynchronously. It works great, but I m doing requests in different controllers and now duplicated methods are in all those controllers. What is the best way to abstract that code (requests) in a single class, so I can easily re-use the code, so I can keep the controllers more simple. I can put it in a singleton (or in the app delegate), but I don t think that s a good approach. Or maybe make my own protocol for it with delegate callback.

Any advice on a good design approach would be helpful. Thanks.

问题回答

I am using subclasses of Ben Copsey s ASIHTTPRequest very heavily for a web service client I hope to finish in the next couple weeks. It s a great project and his work has saved me a great deal of time and effort.

I have an ASINetworkQueue set up in the application delegate. The queue is sent the -go message so that it is ready to receive requests. I add my subclassed requests to this queue. Each request is processed and issues its notifications, and my view controller handles the response data accordingly.

What I have done is subclass ASIHTTPRequest and:

  1. Set up an -init method (or -initWithParams: method, depending on the request)
  2. Override -requestFailed: and -requestCompleted: to handle HTTP error messages coming back from the web service
  3. My view controllers register to observe NSNotification notifications that come from error handling in the -requestCompleted: method

As view controllers are pushed on and popped off the navigation stack, I add and remove different registrations. Some view controllers only need to listen for certain subclassed request types.

Listening for NSNotification allows me to issue UIAlertView dialogs to let the user know something went wrong, or to handle the request response data (feeding results into a Core Data store, for example) when the request yields a successful HTTP error.

Whether the request succeeds or fails, I remember to -release the request when I m done with it.

I ve written a singleton class to handle this.

static  TCHttpRequest *_sharedHttpRequest = nil;
+ (id)sharedRequest
{
    @synchronized(self){
        if (_sharedHttpRequest == nil) {
            _sharedHttpRequest = [[self alloc] init];
        }
    }
    return  _sharedHttpRequest;
}


- (NSDictionary *)loginWithUserName:(NSString *)user password:(NSString *)pwd
{
    NSURL *url = [NSURL URLWithString:@"/login" relativeToURL:_url];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setCachePolicy:ASICacheForSessionDurationCacheStoragePolicy];
    [request setTimeOutSeconds:HTTP_REQ_TIMEOUT];
    [request addPostValue:user forKey:@"username"];
    [request addPostValue:pwd forKey:@"password"];
    request.delegate = self.delegate;
    [_common_queue addOperation:request];
}

I do not see how the class method and the instance method are related? The class method instantiates TCHttpRequest and guarantees only a single instance. The loginWithUserName instance method instantiates a ASIFormDataRequest and adds it to a common queue. I do not see any reuse here?





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签