美文网首页
YTKNetwork 源码分析

YTKNetwork 源码分析

作者: BoxDeng | 来源:发表于2017-09-05 09:59 被阅读0次

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, 我觉得

相关文章

  • YTKNetwork 源码分析

    YTKNetworkAnalysis YTKNetwork 源码分析 1. 功能介绍 1.1 YTKNetwork...

  • YTKNetwork

    YTKNetwork集成教程以及相关问题思考 源码解析之--YTKNetwork网络层 YTKNetwork源码解...

  • YTKNetwork 源码分析

    YTKChainRequest 的 理解key 源码 注释, 很清楚。 /// The success callb...

  • ios三方库解析

    YYCache 源码解析 YTKNetwork 源码解析 MJRefresh 源码解析 VVeboTableVie...

  • iOS 一些框架源码解析

    YYCache 源码解析 YTKNetwork源码解析 MJRefresh 源码解析 VVeboTableView...

  • YTKNetwork 源码 +

    重写这个方法,返回 401, 有趣的变化

  • YTKNetwork中的命令模式

    YTKNetwork中的命令模式 今天开发中,遇到一个新框架 YTKNetwork .下午抽空,读了一下源码.比...

  • YTKNetWork源码解析

    背景: YTKNetWork是一个开源的第三方网络请求框架,具有比较好的网络请求缓存机制的控制。近期项目中想要采取...

  • YTKNetwork源码解析

    对于iOS开发者来说,就算是没有用过YTKNetwork框架,应该也见过,听过了。它是猿题库技术团队开源的一个网络...

  • 【源码阅读】YTKNetwork

    YTKNetwork是一个基于AFNetworking的网络层封装,包括以下几个核心的类 YTKBaseReque...

网友评论

      本文标题:YTKNetwork 源码分析

      本文链接:https://www.haomeiwen.com/subject/kvubettx.html