I am using ASIHTTPRequest framework for making network calls in my iOS applications. But I don’t want to use it directly in all the controllers in my application. So I thought of writing a layer around ASIHTTPRequest. My code use this layer to use ASIHTTPRequest. The benefit wil be in future I should be able to replace this framework with some other framework and my code will be unchanged just the layer would change. I want to know what should be the strategy to do so. Should I subclass my class from ASIHTTPRequest class, or implement my own class. What should be the method I should consider implementing.
Currently I am implementing it like this.
My wrapper is
MyRequestHandler.h : NSObject
@property ASIHTTPRequest *asiHttpReq;
-(void) sendAsyncGetRequest
{
self.asiRequest = [ASIHTTPRequest requestWithURL:self.url];
if(self.tag != 0){
self.asiRequest.tag = self.tag;
}
[self.asiRequest setDelegate:self];
[self.asiRequest startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request{
MyResponseObj *respone = <From request obj>
if([delegate respondsToSelector:@selector(requestFinished:)]){
[delegate performSelector:@selector(requestFinished:) withObject:response];
}
}
And in my viewcontroller I would do this:
MyViewController.h : UIViewContoller
@property MyRequestHandler *reqHandler;
-(void) fireRequest
{
NSString* requestUrl = <create URL>;
if(requestUrl){
// [self showLoadingIndicatorView];
// Proceed for request.
NSURL *url = [NSURL URLWithString:requestUrl];
reqHandler = [MyRequestHandler requestWithURL:url];
reqHandler.tag = 1000;
[reqHandler setDelegate:self];
[reqHandler sendAsyncGetRequest];
}
}
- (void)requestFinished:(MyResponse*) responseData{
// Do Your parsing n all here.
}
- (void)requestFailed:(MyResponse*) responseData{
// Handle the error here.
}
Is this the right way to do it. The problem here is as I have created property of myrequesthandler in viewcontroller I can only make one request at a time, and loosing the capability of ASIHTTPRequest of making multiple request simultaneously.
Can you suggest me how to approach problems like this.
This is what I’m using:
//
To use it, just import
RequestPerformer.hin yourUIViewControllerand do smth like:Parameters:
(NSString *)string– url string, where to post your request;(NSDictionary *)stringDictionary– dictionary, which contains all thetext information (such as name, id etc.);
(NSDictionary *)dataDictionary– dictionary, which contains all data information (such as photos, files, etc.);(id)requestDelegate– delegate to perform selectors below;(SEL)requestSelector– selector, which will be executed while successfully request;(SEL)errorSelector– selector, which will be executed, while error occurred.