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 的 優(yōu)化



- (void)loginButtonPressed:(id)sender {

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

?[api ?startWithCompletionBlockWithSuccess: ?^(YTKBaseRequest ?* ?request) {

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

NSLog(@"succeed");? ? ? ??

}

failure:^(YTKBaseRequest *request) {

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

NSLog(@"failed");? ? ??

? }];?

?? }}


注意:你可以直接在 block 回調(diào)中使用self,不用擔(dān)心循環(huán)引用。因?yàn)?YTKRequest 會(huì)在執(zhí)行完 block 回調(diào)之后,將相應(yīng)的 block 設(shè)置成 nil。從而打破循環(huán)引用。

源碼:?

@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;

}



校驗(yàn),支持檢查返回 JSON 內(nèi)容的合法性

?使用了Key-Value Validation, 我覺得

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,116評論 0 23
  • 2017.8.11 剛開學(xué),經(jīng)過五天的學(xué)習(xí),分班帶來的更多的不是陌生感,我覺得是壓力。真的,自我感覺,現(xiàn)在的班跟以...
    ice_bj閱讀 668評論 0 0
  • 1. 我的一個(gè)小學(xué)同學(xué),拍了兩張照片發(fā)到群里。 他@了我們一個(gè)女同學(xué),讓她看看這是哪里。 女同學(xué)激動(dòng)地說:這是我老...
    653e0adfb5bf閱讀 1,994評論 0 1
  • 這個(gè)標(biāo)題,來源于我高中的記憶。一段時(shí)間,班主任要每天換一個(gè)人在黑板的最右側(cè)寫上一個(gè)自己認(rèn)為好的名言警語。記得班主任...
    luckcul閱讀 565評論 0 3
  • 開篇 1到底這個(gè)序列化有何作用?面向?qū)ο蟮某绦蛟谶\(yùn)行的時(shí)候會(huì)創(chuàng)建一個(gè)復(fù)雜的對象圖,經(jīng)常要以二進(jìn)制的方法序列化這個(gè)對...
    ch123閱讀 1,301評論 0 1

友情鏈接更多精彩內(nèi)容