In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today I will show you how to interpret the AFNetworking framework. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
Do ios development, AFNetworking this network framework must be very familiar, maybe we usually only use part of its functions, and we are not very clear about its implementation principle, as if there is always a fog in front of us.
Next, we will read the code of this framework in great detail. our goal is to understand how our request is realized and where our code needs to be improved. if we can go further, we can sum up a set of network architecture ideas suitable for most applications.
Can make some people benefit from it.
Let's first take a look at the file system of the entire framework, without explaining the role of each file, we will summarize the entire framework in the last article of the entire source code interpretation. There will be a clear chart to illustrate the problem.
When we look at a frame, we can first look at the header file of each file, that is, the .h file.
As you can see, some header files contain other header files. Without considering the header files of the system, we can find some relatively independent classes. From the figure above, we can see that
The more independent classes are:
1.AFURLResponseSerialization.h
2.AFNetworkReachabilityManager.h
3.AFURLRequestSerialization.h
4.AFSecurityPolicy.h
Here is the content of AFNetworkReachabilityManager.h, a class used to monitor changes in the network environment.
# import
By importing this header file, we know that the implementation of network monitoring depends on the api of SystemConfiguration. This shows that this api can provide such a function, at least let us understand that we usually import it for a purpose.
Whether this is an enumeration encapsulation or follows a principle of using enumerations, we consider enumerations when satisfying a limited collection with a unified theme. Here the author enumerates four types. These types can meet most of the functions in our development, and if not, you can extend them on your own.
NS_ASSUME_NONNULL_BEGINNS_ASSUME_NONNULL_END
This is added for the optional type of swift, and the parameters of the content at both ends are nonnull by default.
This passage is a description of this class. Let's just ignore what it said and take a look at Apple's official
Such content appears above a property or method for the purpose of interpreting its content. When I see this, I think of our usual development. We can treat each piece of code as the development of api, and write notes in more detail. I have seen two different arguments, one is to make the code comments as little as possible, requiring the code introduction to be readable. The other is to say that the comments should be detailed, focusing on how others feel about reading the code. I think it's better to write in more detail, because after a while, I may not remember the code I wrote at that time. It is possible to think of something in the process of writing these tedious comments, such as how to merge unnecessary methods, and so on.
This class provides four read-only properties to let us get what we need.
1. Network statu
two。 Is it reachable?
3. Whether the current connection is WWAN
4. Is the current connection WiFi enough?
All four properties are read-only and only give the user access. Note that the BOOL property generally writes the getter method.
The author uses this to separate different functional modules in the same class. This is a matter of personal habit. As an example of usual development, I personally use # pragma mark to separate different functions in the .m file.
5 initialization methods are provided, which can meet most of the requirements.
SCNetworkReachabilityRef is very important, and this class is developed based on it.
+ (instancetype) managerForDomain: (NSString *) domain; listens to determine the network status of domain.
+ (instancetype) managerForAddress: (const void *) address; monitors the network status of a socket address. For socket communications, please see this article: socket Communications.
The way to turn monitoring on and off.
Returns a string in the local language of the network status. Often we can use this string to tell the user what is going on on the current network, of course, we can also customize the prompt text according to the status.
There are two ways to set the callback for network transition changes and listen for network changes:
1. Use the method above.
two。 Monitor AFNetworkingReachabilityDidChangeNotification notifications.
This is a notification related to a change in the state of the network. One of the accepted notifications will have a userinfo that is a NSDictionary, where key is
AFNetworkingReachabilityNotificationStatusItem
What these simple two lines of code can tell us is that whenever we design the notification function in our usual development, we should package the notification string into a proprietary file and distinguish it according to different modules within the file. of course, the necessary comments are also necessary.
Ps: both FOUNDATION_EXPORT and # define can define constants. FOUNDATION_EXPORT can use = = to make judgments, which is slightly more efficient. And can hide the definition details (that is, the implementation part is not there. Middle)
To the function: get the string declaration based on the state.
Well, we have been very confident in interpreting the .h file of this class, and we are not talking about the functions it provides, but what we can think of and what can help us program better by reading each line of code.
Let's move on to the contents of AFNetworkReachabilityManager.m.
These header files are system libraries and are prepared for the following sockaddr_in6 / sockaddr_in. If you are not familiar with this article, you can read the socket communication.
There's nothing more to say about these. Let's move on.
This method is an implementation of the last method in .h. It means that we pay attention to the macro NSLocalizedStringFromTable. Why pay attention to it?
This involves the issue of local internationalization. The so-called internationalization is to enable your app to display the corresponding language according to different languages.
* but this is not easy. Inexperienced developers may not make such a setting at first, but it will be troublesome to do so if they need to do it internationally in the future. Therefore, in the opening, wherever strings are used, different languages should be taken into account. In different languages, the length of the string used for the expression of a meaning is different, which implies that the width of the space may be different.
All right, the content of internationalization will not be said, please search by yourself.
1 / * * 2 * according to the network tag SCNetworkReachabilityFlags, it is transformed into the network state 3 1 that we often use in development. Unable to connect to network 4 2. Cellular connection 5 3.WiFi connection 6 4. Unknown connection 7 * / 8 static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags (SCNetworkReachabilityFlags flags) {9 10 / / can reach 11 BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable)! = 0); 12 13 / / need to establish a connection before networking 14 BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired)! = 0) 15 16 / whether you can automatically connect 17 BOOL canConnectionAutomatically = ((flags & kSCNetworkReachabilityFlagsConnectionOnDemand)! = 0) | ((flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)! = 0); 18 19 / / whether you can connect, 20 BOOL canConnectWithoutUserInteraction = (canConnectionAutomatically & & (flags & kSCNetworkReachabilityFlagsInterventionRequired) = 0) without manual setting by the user; 21 22 / / condition 1 of whether you can connect to the Internet. Can reach 2. It means that you can connect to the network 23 BOOL isNetworkReachable = (isReachable & & (! needsConnection | | canConnectWithoutUserInteraction)); 24 25 AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;26 if (isNetworkReachable = = NO) {27 status = AFNetworkReachabilityStatusNotReachable;28} 29 # if TARGET_OS_IPHONE30 else if ((flags & kSCNetworkReachabilityFlagsIsWWAN)! = 0) {31 status = AFNetworkReachabilityStatusReachableViaWWAN 32} 33 # endif34 else {35 status = AFNetworkReachabilityStatusReachableViaWiFi;36} 37 38 return status;39}
This method converts to our custom enumerated type based on the SCNetworkReachabilityFlags tag. As for the conversion rules, the comments in the above code are very clear.
* I have to say a few more words here. Private methods in a class are written like this in many frameworks. Why? We often write proprietary methods like-(void) funcName; in our development.
My personal opinion is that it is better to write a private method in a class as a c function such as static void funcName ().
1. At the front of the file, it's easier to find.
two。 Inline functions can be used appropriately to improve efficiency.
Block and notifications are handled according to an identity. Make sure both are in the same state.
Contains the properties that need to be handled in the class.
Take a look at the most basic initialization method, initializing its own properties.
Remember CFRelease () after CFRetain ().
Initialized with an socket address. First create a new SCNetworkReachabilityRef object, and then call the initWithReachability: method. Remember to manage memory manually.
This method is basically the same as above.
Combining the above two methods, we find that there are two ways to create SCNetworkReachabilityRef:
1. SCNetworkReachabilityCreateWithName
2. SCNetworkReachabilityCreateWithAddress
Since IPv6 was launched after ios9 and os_x 10.11, it is necessary to determine the version. For the knowledge of socket designed this week, please see socket Communications.
What can we learn from this code?
1, the creation of methods is also in order, you can use the idea of using functions to access functions.
2. A precompiled instruction such as @ if can replace part of the if else in the code. The advantage is the difference between whether the code will be compiled or not.
The way to write a single case.
Do some treatment when it needs to be released.
This is the getter method of the three BOOL attributes exposed in the .h file. Note that since we defined the getter method in @ property, the getter method will be written as we defined it.
From these three methods, we can also see that the idea of function nesting is still very important. If you want to do this, you can only think more about it.
This is the core method of this class, setting up a listening network to listen.
Let's learn the basics first.
SCNetworkReachabilityContext
If you click in, you will find that this is a structure. Generally, the structure of c language is a description of the data to be saved.
1. The first parameter accepts a parameter of signed long
two。 The second parameter accepts a value of type void *, which is equivalent to the id type of oc. Void * can point to any type of parameter.
3. The third argument is a function designed to retain info.
4. The fourth argument is a function designed to release info.
5. The fifth argument is a function that gets the Description string according to info
The info we are going to carry here is the block below.
1 _ weak _ typeof (self) weakSelf = self; 2 AFNetworkReachabilityStatusBlock callback = ^ (AFNetworkReachabilityStatus status) {3 _ strong _ typeof (weakSelf) strongSelf = weakSelf; 4 5 strongSelf.networkReachabilityStatus = status; 6 if (strongSelf.networkReachabilityStatusBlock) {7 strongSelf.networkReachabilityStatusBlock (status); 8} 9 10}
The retain and release functions are the following two functions
1 static const void * AFNetworkReachabilityRetainCallback (const void * info) {2 return Block_copy (info); 3} 4 5 static void AFNetworkReachabilityReleaseCallback (const void * info) {6 if (info) {7 Block_release (info); 8} 9}
Setting up network monitoring is divided into the following steps:
1. Let's create a new context first.
1 SCNetworkReachabilityContext context = {0, (_ _ bridge void *) callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL}
two。 Set callback
1 SCNetworkReachabilitySetCallback (self.networkReachability, AFNetworkReachabilityCallback, & context)
Where the AFNetworkReachabilityCallback is a function defined in this way
Typedef void (* SCNetworkReachabilityCallBack) (SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void * _ _ nullable info)
In this category
1 static void AFNetworkReachabilityCallback (SCNetworkReachabilityRef _ unused target, SCNetworkReachabilityFlags flags, void * info) {2 AFPostReachabilityStatusChange (flags, (_ bridge AFNetworkReachabilityStatusBlock) info); 3}
3. Join the RunLoop pool
1 SCNetworkReachabilityScheduleWithRunLoop (self.networkReachability, CFRunLoopGetMain (), kCFRunLoopCommonModes)
Where CFRunLoopGetMain () represents the main RunLoop
Ok, it's almost done.
The current network state is sent once in an asynchronous thread.
Stop network monitoring
There is nothing more to say about these two methods. One is getter and the other is setter.
Registration key value dependence, this may be that we usually use less. You can learn about it.
For example, a class User has two attributes
There is also a card class card.
Let's write a setter and getter method for info
1 @ interface User: NSObject 2 @ property (nonatomic,copy) NSString * name; 3 @ property (nonatomic,assign) NSUInteger age; 4 @ end 5 6 7 8 @ interface card: NSObject 9 @ property (nonatomic,copy) NSString * info;10 @ property (nonatomic,strong) User * user;11 @ end12 @ implementation card13 14-(NSString *) info {15 return [NSString stringWithFormat:@ "% @ /% lu", _ user.name, (unsigned long) _ user.age] 16} 17-(void) setInfo: (NSString *) info {18 19 NSArray * array = [info componentsSeparatedByString:@ "/"]; 20 _ user.name = array [0]; 21 _ user.age = [array [1] integerValue]; 22 23} 24 25 + (NSSet *) keyPathsForValuesAffectingValueForKey: (NSString *) key {26 NSSet * keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; 27 NSArray * moreKeyPaths = nil 28 29 if ([key isEqualToString:@ "info"]) 30 {31 moreKeyPaths = [NSArray arrayWithObjects:@ "user.name", @ "user.age", nil]; 32} 33 34 if (moreKeyPaths) 35 {36 keyPaths = [keyPaths setByAddingObjectsFromArray:moreKeyPaths]; 37} 38 39 return keyPaths;40} 41 42 @ end
Gh.dokee.cn/article/content-2292769-34004.html
Gh.dokee.cn/article/content-2292768-34004.html
Gh.dokee.cn/article/content-2292766-34004.html
Gh.dokee.cn/article/content-2292765-34004.html
Gh.dokee.cn/article/content-2292764-34004.html
Gh.dokee.cn/article/content-2292763-34004.html
Gh.dokee.cn/article/content-2292762-34004.html
Gh.dokee.cn/article/content-2292761-34004.html
Gh.dokee.cn/article/content-2292760-34004.html
Gh.dokee.cn/article/content-2292759-34004.html
Gh.dokee.cn/article/content-2292758-34004.html
Bbs.open.qq.com/thread-15334805-1-1.html
Bbs.open.qq.com/thread-15335348-1-1.html
Bbs.open.qq.com/thread-15335576-1-1.html
Bbs.open.qq.com/thread-15335715-1-1.html
Http://bbs.open.qq.com/thread-15335916-1-1.html
Http://bbs.open.qq.com/thread-15335876-1-1.html
Http://bbs.open.qq.com/thread-15336398-1-1.html
Http://bbs.open.qq.com/thread-15336484-1-1.html
Http://bbs.open.qq.com/thread-15336547-1-1.html
Http://bbs.open.qq.com/thread-15336614-1-1.html
Http://bbs.open.qq.com/thread-15336697-1-1.html
Http://bbs.open.qq.com/thread-15336806-1-1.html
Http://bbs.open.qq.com/thread-15340763-1-1.html
Www.baiyewang.com/s4209086.html
Http://bl.gamebbs.qq.com/forum.php?mod=viewthread&tid=11675819
Http://bbs.open.qq.com/thread-15343974-1-1.html
Http://bbs.open.qq.com/thread-15344107-1-1.html
Http://caimi68.lofter.com/post/1e3e0a7a_bbb5392
Http://caimi68.lofter.com/post/1e3e0a7a_bbb539f
Http://caimi68.lofter.com/post/1e3e0a7a_bbb539e
Http://caimi68.lofter.com/post/1e3e0a7a_bbb539d
Http://caimi68.lofter.com/post/1e3e0a7a_bbb539c
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a0
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a1
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a2
Http://caimi68.lofter.com/post/1e3e0a7a_bbb53a4
Http://bbs.open.qq.com/thread-15345770-1-1.html
Http://bbs.open.qq.com/thread-15345813-1-1.html
Http://bbs.open.qq.com/thread-15345854-1-1.html
Http://bbs.open.qq.com/thread-15345888-1-1.html
Http://bbs.open.qq.com/thread-15345937-1-1.html
Http://bbs.open.qq.com/thread-15346013-1-1.html
Http://bbs.open.qq.com/thread-15346046-1-1.html
Http://bbs.open.qq.com/thread-15346098-1-1.html
Http://bbs.open.qq.com/thread-15346138-1-1.html
Http://bbs.open.qq.com/thread-15346194-1-1.html
Http://bbs.open.qq.com/thread-15346240-1-1.html
Http://bbs.open.qq.com/thread-15346345-1-1.html
Http://g.jandan.net/s/6319
Http://g.jandan.net/s/6320
Http://g.jandan.net/s/6321
Http://g.jandan.net/s/6322
Http://g.jandan.net/s/6323
The above is how to parse the whole content of the interpretation of AFNetworking framework, more content related to how to interpret the interpretation of AFNetworking framework can search the previous articles or browse the following articles to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.