AFNetWorking初探之AFHTTPRequestOperation(三)

本文详细探讨了AFHTTPRequestOperation的setCompletionBlockWithSuccess方法,包括其内部工作原理、如何回调以及如何维护线程。通过源码分析,揭示了该方法如何在请求成功或失败时调用相应的block,并且介绍了completionBlock属性在NSOperation中的作用,以及AFURLConnectionOperation中的线程管理和并发控制机制。
摘要由CSDN通过智能技术生成

关于AFHTTPRequestOperation 之前已经说说其使用方法,以及一些固定的包装初始化的操作,今天我们深入到setCompletionBlockWithSuccess看看它到底怎样的去回调,怎样的维护线程。首先贴出源码。

.h中的说明

/**
 Sets the `completionBlock` property with a block that executes either the specified success or failure block, depending on the state of the request on completion. If `error` returns a value, which can be caused by an unacceptable status code or content type, then `failure` is executed. Otherwise, `success` is executed.

 This method should be overridden in subclasses in order to specify the response object passed into the success block.

 @param success The block to be executed on the completion of a successful request. This block has no return value and takes two arguments: the receiver operation and the object constructed from the response data of the request.
 @param failure The block to be executed on the completion of an unsuccessful request. This block has no return value and takes two arguments: the receiver operation and the error that occurred during the request.
 */
- (void)setCompletionBlockWithSuccess:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                              failure:(nullable void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;


用一个block设置complerionblock 属性,成功还是失败的block都取决于完成时的请求码。如果“error”返回一个值,由于不能接受的状态码或者内容类型导致的,这时“failure”将被执行,其他情况“success”将被执行。

这个方法应该被子类覆盖,为了指定被传递进去的成功block响应对象。





.m中的实现

- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                              failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    // completionBlock is manually
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值