YTKNetwork 源码分析

YTKChainRequest

的 理解key

 源码 注释, 很清楚。


///  The success callback. Note if this value is not nil and `requestFinished` delegate method is

///  also implemented, both will be executed but delegate method is first called. This block

///  will be called on the main queue.

@property (nonatomic, copy, nullable) YTKRequestCompletionBlock successCompletionBlock;

///  The failure callback. Note if this value is not nil and `requestFailed` delegate method is

///  also implemented, both will be executed but delegate method is first called. This block

///  will be called on the main queue.

@property (nonatomic, copy, nullable) YTKRequestCompletionBlock failureCompletionBlock;



block 的 优化



- (void)loginButtonPressed:(id)sender {

    RegisterApi *api = [[RegisterApi  alloc ]  init];       

 [api  startWithCompletionBlockWithSuccess:  ^(YTKBaseRequest  *  request) {

//你可以直接在这里使用 self

NSLog(@"succeed");        

}

failure:^(YTKBaseRequest *request) {

//你可以直接在这里使用 self

NSLog(@"failed");      

  }]; 

   }}


注意:你可以直接在 block 回调中使用self,不用担心循环引用。因为 YTKRequest 会在执行完 block 回调之后,将相应的 block 设置成 nil。从而打破循环引用。

源码: 

@implementation YTKRequest

- (void)start {

if (self.ignoreCache) {

[self startWithoutCache];

return;}

// Do not cache download request.

if (self.resumableDownloadPath) {

[self startWithoutCache];

return;}

if (![self loadCacheWithError:nil]) {

[self startWithoutCache];

return;}

_dataFromCache = YES;

dispatch_async(dispatch_get_main_queue(), ^{

[self requestCompletePreprocessor];

[self requestCompleteFilter];

YTKRequest *strongSelf = self;

[strongSelf.delegate requestFinished:strongSelf];

if (strongSelf.successCompletionBlock) {

strongSelf.successCompletionBlock(strongSelf);}

[strongSelf clearCompletionBlock];  // nil out to break the retain cycle.

});

}


- (void)clearCompletionBlock {

// nil out to break the retain cycle.

self.successCompletionBlock = nil;

self.failureCompletionBlock = nil;

}



校验,支持检查返回 JSON 内容的合法性

 使用了Key-Value Validation, 我觉得

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,487评论 0 23
  • 2017.8.11 刚开学,经过五天的学习,分班带来的更多的不是陌生感,我觉得是压力。真的,自我感觉,现在的班跟以...
    ice_bj阅读 3,795评论 0 0
  • 1. 我的一个小学同学,拍了两张照片发到群里。 他@了我们一个女同学,让她看看这是哪里。 女同学激动地说:这是我老...
    653e0adfb5bf阅读 5,896评论 0 1
  • 这个标题,来源于我高中的记忆。一段时间,班主任要每天换一个人在黑板的最右侧写上一个自己认为好的名言警语。记得班主任...
    luckcul阅读 3,424评论 0 3
  • 开篇 1到底这个序列化有何作用?面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对...
    ch123阅读 4,979评论 0 1