解析AFNetWorking 网络框架(二)

接着前面写。

本帖先讲AFURLConnectionOperation,它是AFNetworking中是所有请求网络的基类。它是基于NSURLConnection实现的,所以在此类里实现了NSURLConnection的delegate 方法:

 - `connection:didReceiveResponse:`

 - `connection:didReceiveData:`

 - `connectionDidFinishLoading:`

 - `connection:didFailWithError:`

 - `connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:`

 - `connection:willCacheResponse:`

 - `connectionShouldUseCredentialStorage:`

 - `connection:needNewBodyStream:` 

 - `connection:willSendRequestForAuthenticationChallenge:`

AFURLConnectionOperation是继承于 NSOperation, NSOPeration是为了运用于多线程类,只要将operation加入队列里,operation就会自动执行。另外AFURLConnectionOperation除了实现NSURLConnectionDelegate和NSURLConnectionDataDelegate外,还实现了NSCoding和NSCoding协议,这两个协议主要是将operation归档在硬盘disk,并且copy一份到内存。

下面是AFURLConnectionOperation的属性:

@property (nonatomic, strong) NSSet *runLoopModes;这个默认值是NSRunLoopCommonModes,

@property (readonly, nonatomic, strong) NSURLRequest;这个就是网络request,用于网络连接。

@property (readonly, nonatomic, strong) NSURLResponse *response;这个request请求网络服务器后最近的一次响应,里面包含statusCode错误码,还有该request的所有header信息。

@property (readonly, nonatomic, strong) NSError *error;如果发生错误,包含了该request的错误信息。

@property (readonly, nonatomic, strong) NSData *responseData;就是request网络返回的数据,如果是二进制流可以从此属性读取。

@property (readonly, nonatomic, copy) NSString *responseString;这个属性其实和responseData属性内的数据一样,只不过这个属性石返回的NSString类型,通常获取xml或json数据。

@property (readonly, nonatomic, assign) NSStringEncoding responseStringEncoding;这个属性标识此属性responseString的编码类型,默认是NSUTF8StringEncoding,网络数据通常采用此编码格式。

@property (nonatomic, assign) BOOL shouldUseCredentialStorage;这个属性目前没用过,应该标示是否允许信任连接存储证书(如果有误,请指正)

@property (nonatomic, strong) NSURLCredential *credential;一会查阅再写。

@property (nonatomic, strong) AFSecurityPolicy *securityPolicy;安全策略。

@property (nonatomic, strong) NSInputStream *inputStream;向网络发送的流

@property (nonatomic, strong) NSOutputStream *outputStream;是请求返回的所有数据流保存到缓冲中,这个缓冲就是responseData属性内的数据,如果设置此属性,那么缓冲就不会存储到responseData里,值为nil。

@property (nonatomic, strong) dispatch_queue_t completionQueue;默认为空,主队列被调用,主要用于CompletionBlock。

@property (nonatomic, strong) dispatch_group_t completionGroup;默认为空,一个private dispatch group被使用,主要用于CompletionBlock。

@property (nonatomic, strong) NSDictionary *userInfo;可以保存一些用户信息,与每个request唯一对应。

总结:这些属性里使用最频繁的就request,response,responseData,responseString,userInfo,error。

下面是AFURLConnectionOperation的一些方法:

- (instancetype)initWithRequest:(NSURLRequest *)urlRequest;

参数为一个 urlRequest返回一个AFURLConnectionOperation的实例, instancetype类似于id,ios7以后apple换成这。

- (void)pause;暂停一个正在准备ready,executing,finished的operation,保留在队列里直到被取消或者重启resume,暂停一个已完成的或者取消的,已暂停的operation不会有任何作用。

- (BOOL)isPaused;这个不用解释返回yes或no。

- (void)resume;这个restart一个paused的operation。从队列里移除。

- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler;app切换到后台还会执行。(这个目前还没用过)

- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block;

向服务器上传二进制数据流的回调block。

- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block;

从服务器下载二进制数据流的回调block。

- (void)setWillSendRequestForAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block;

这个没用过,然后补充,可能是鉴权之类的把。

- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block;

这个应该是重定向时执行。

- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block;

从缓冲获取response调用。

+ (NSArray *)batchOfRequestOperations:(NSArray *)operations

                        progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock

                      completionBlock:(void (^)(NSArray *operations))completionBlock;

这个没用过,然后补充。


下面是一些extern的变量:

extern NSString * const AFNetworkingErrorDomain;

extern NSString * const AFNetworkingOperationFailingURLRequestErrorKey;

extern NSString * const AFNetworkingOperationFailingURLResponseErrorKey;


///--------------------

/// @name Notifications

///--------------------


/**

 Posted when an operation begins executing.

 */

extern NSString * const AFNetworkingOperationDidStartNotification;


/**

 Posted when an operation finishes.

 */

extern NSString * const AFNetworkingOperationDidFinishNotification;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值