A small architecture example of iOS-encapsulation of asynchronous requests

Source: Internet
Author: User

The Forum saw the question of a middleware: "Can an asynchronous request encapsulate a common class? Each page needs to copy several methods, which makes it very troublesome.

Many people may have the same problem, so I started a blog to talk about this issue. I mainly want to elaborate on programming ideas and architecture design. Let's talk a little bit about it. Go directly to the Code:

Myurlclass. h

#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

So far, my "asynchronous request class" is encapsulated. mdelegates is used to save the request object (that is, the class object that sends the asynchronous request, in this way, the callback method can be called during asynchronous connectiondidfinishloading to continue:

- (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); }

The above code is the main piece of code for sending the request class.

1. click the button to create a new object of the myurlclass class, add itself to the mdelegates of myurlclass, send an asynchronous request, and set the delegate of the request to the object of myurlclass.

2. After the request is responded, the connectiondidfinishloading method in the myurlclass class will be called. In this method, the object of the request class will be found, and its finishwithdata method will be called and mdata will be passed, at this point, a complete HTTP Communication ends. Of course, do not forget to remove yourself from the set after the request ends (call removedelegate: Self)

The code above is just a small example I have written. It is mainly used to reflect the idea. When encapsulated and applied to projects, you must note that: 1. You need to lock adddelegate and removedelegate; 2. Data processing should be performed first, such as converting to nsdictionary and passing it to finishwithdata. Of course, do not forget to change the finishwithdata parameter type. 3. If you do not want all callback methods to be named finishwithdata, then you can perform a relational ing (both struct and nsdictionary can meet your needs), respectively store the correspondence between each class and the callback method name, and modify Sel
Seltor = @ selector (finishwithdata :); implementation here; 4. If you want to write an encapsulated myurlclass, you must make it a global object, do not consider alloc in sendmyrequest as above.

In fact, there are still many areas that can be encapsulated. For example, the sendmyrequest method has the potential to be encapsulated.

I briefly introduced a small architecture and hope it will be helpful to you. You are also welcome to discuss it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.