The implementation of asynchronous request encapsulation in iOS development architecture is the content to be introduced in this article. It mainly refers to the encapsulation of asynchronous requests in iOS development. Here, we will explain in detail.
I am looking for content related to iOS development. I have read this article and share it with my friends. The Forum saw the question of a middleware: "Can an asynchronous request be encapsulated into a public class? Each page needs to copy several methods, which is 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 the programming ideas and architecture design, so that I can give it a reference. 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 );
}
-(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 wrote. It is mainly used to reflect the idea. When encapsulated and applied to projects, pay attention to the following:
1. Lock addDelegate and removeDelegate;
2. Data processing should be performed first, for example, 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, you can create a relational ing (both struct and NSDictionary can meet your needs ), store the correspondence between each class and the callback method name, and modify SEL seltor = @ selector (finishWithData :); The implementation here;
4. If you want to write a encapsulated MyURLClass, you must make it a global object. Do not want to show 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.
Summary: The implementation of the iOS development architecture encapsulation asynchronous request case has been introduced, that is, a simple introduction to a small architecture. It is best to hope this article will help you!
From Yongzheng's first class, my faith!