IOS开发架构封装异步请求案例实现
IOS开发架构封装异步请求案例实现是本文要介绍的内容,主要是实现IOS开发中异步请求的封装案例,来看本文详细内容讲解。
在找IOS开发相关的内容是,看到本文,与朋友们分享一下。论坛里看到一位兄台的问题:”异步请求能不能封装诚一个公用的类?每个页面都要copy一下几个方法,感觉很麻烦“可能很多人也会有相同的问题,于是起一篇博客专门谈谈这个问题,主要想阐述一下编程思想及架构设计,权当抛砖引玉。闲话不多说,直接上代码:
MyURLClass.h #import <Foundation/Foundation.h> @interface MyURLClass : NSObject { @private NSMutableSet *mDelegates; NSMutableData *mData; } - (void) addDelegate:(id)delegate; - (void) removeDelegate:(id)delegate; @end #import <Foundation/Foundation.h> @interface MyURLClass : NSObject { @private NSMutableSet *mDelegates; NSMutableData *mData; } - (void) addDelegate:(id)delegate; - (void) removeDelegate:(id)delegate; @end MyURLClass.m @implementation MyURLClass - (id)init { [super init]; mDelegates = [NSMutableSet new]; mData = [NSMutableData new]; return self; } - (void) addDelegate:(id)delegate { [mDelegates addObject:delegate]; } - (void) removeDelegate:(id)delegate{ [mDelegates removeObject:delegate]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [mData appendData:data]; } - (void) connectionDidFinishLoading : (NSURLConnection *) connection { NSLog(@"delegate:%@", mDelegates); SEL seltor = @selector(finishWithData:); NSSet * set = [[NSSet alloc] initWithSet:mDelegates]; for (id del in set) { if ([del respondsToSelector:seltor]) [del performSelector:seltor withObject:mData]; } [set release]; } @end @implementation MyURLClass - (id)init { [super init]; mDelegates = [NSMutableSet new]; mData = [NSMutableData new]; return self; } - (void) addDelegate:(id)delegate { [mDelegates addObject:delegate]; } - (void) removeDelegate:(id)delegate{ [mDelegates removeObject:delegate]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [mData appendData:data]; } - (void) connectionDidFinishLoading : (NSURLConnection *) connection { NSLog(@"delegate:%@", mDelegates); SEL seltor = @selector(finishWithData:); NSSet * set = [[NSSet alloc] initWithSet:mDelegates]; for (id del in set) { if ([del respondsToSelector:seltor]) [del performSelector:seltor withObject:mData]; } [set release]; } @end
至此,我的“异步请求类”就封装好了,mDelegates是用来保存请求对象(即发送异步请求的那个类的对象)的,以便在异步connectionDidFinishLoading的时候调用回调方法,继续:
- (IBAction)sendMyRequest { NSURL *url = [NSURL URLWithString:@"http://www.csdn.net"]; NSURLRequest *mRequest = [NSURLRequest requestWithURL:url]; MyURLClass *urlClass = [[MyURLClass alloc] init]; [urlClass addDelegate:self]; [[NSURLConnection alloc] initWithRequest:mRequest delegate:urlClass startImmediately:YES]; } - (void)finishWithData:(NSData *)data { NSLog(@"%@", data); } - (IBAction)sendMyRequest { NSURL *url = [NSURL URLWithString:@"http://www.csdn.net"]; NSURLRequest *mRequest = [NSURLRequest requestWithURL:url]; MyURLClass *urlClass = [[MyURLClass alloc] init]; [urlClass addDelegate:self]; [[NSURLConnection alloc] initWithRequest:mRequest delegate:urlClass startImmediately:YES]; } - (void)finishWithData:(NSData *)data { NSLog(@"%@", data); }
上面这段代码就是发送请求类的主要一段代码
1、点击按钮后,新建一个MyURLClass类的对象,然后将自己添加到MyURLClass的mDelegates中,之后发送异步请求,并且发送请求的delegate设为MyURLClass的对象
2、请求响应后,就会调用MyURLClass类中的connectionDidFinishLoading方法,在这个方法中会找到发送请求类的对象,并且调用他的finishWithData方法,同时传递mData这个参数,至此,一个完整的http通信结束,当然不要忘了请求结束把自己从set中移除(调用removeDelegate:self)
上面的代码只是我写的一个小例子,主要为了体现思想,真正要是封装并且运用到项目中的时候一定要注意:1、在addDelegate和removeDelegate的时候需要加锁;2、应该先进行数据处理,比如转换为NSDictionary在传递给finishWithData,当然,不要忘了改finishWithData的参数类型;3、如果你不想所有的回调方法名都叫finishWithData,那你可以做一个关系映射(struct和NSDictionary都可以满足你的需求),分别存储每个类与回调方法名的对应,同时需要修改SEL seltor = @selector(finishWithData:);这里的实现;4、如果你要写一个封装好的MyURLClass,一定要做成一个全局对象,千万不要想我上面那样在sendMyRequest中现alloc。