生活随笔
收集整理的這篇文章主要介紹了
AFNetworking 3.1.0 使用中某些知识点讲解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
# POST / GET ?請求
??/*!
?? ? 首先要知道,POST請求不能被緩存,只有 GET 請求能被緩存。因為從數(shù)學的角度來講,GET 的結果是 冪等 的,就好像字典里的 key 與 value 就是冪等的,而 POST 不 冪等 。緩存的思路就是將查詢的參數(shù)組成的值作為 key ,對應結果作為value。從這個意義上說,一個文件的資源鏈接,也叫 GET 請求,下文也會這樣看待。
?? ? 80%的緩存需求:兩行代碼就可滿足
?? ? 設置緩存只需要三個步驟:
? ? ?
?? ? 第一個步驟:請使用 GET 請求。
? ? ?
?? ? 第二個步驟:
? ? ?
?? ? 如果你已經(jīng)使用 了 GET 請求,iOS 系統(tǒng) SDK 已經(jīng)幫你做好了緩存。你需要的僅僅是設置下內存緩存大小、磁盤緩存大小、以及緩存路徑。甚至這兩行代碼不設置也是可以的,會有一個默認值。代碼如下:
?? ? 要注意
?? ? iOS 5.0開始,支持磁盤緩存,但僅支持 HTTP
?? ? iOS 6.0開始,支持 HTTPS 緩存
?? ? ?*/
? ? NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
? ? [NSURLCache setSharedURLCache:urlCache];
#########
/**? ?*??取消所有的網(wǎng)絡請求? ?*??a?finished?(or?canceled)?operation?is?still?given?a?chance?to?execute?its?completion?block?before?it?iremoved?from?the?queue.? ?*/?? ?? +(void)cancelAllRequest?? {?? ????[[BJAppClient?sharedClient].operationQueue?cancelAllOperations];?? }?? ?? ?? ?? #pragma?mark?-???取消指定的url請求/?? /**? ?*??取消指定的url請求? ?*? ?*??@param?requestType?該請求的請求類型? ?*??@param?string??????該請求的完整url? ?*/?? ?? +(void)cancelHttpRequestWithRequestType:(NSString?*)requestType?? ???????????????????????requestUrlString:(NSString?*)string?? {?? ????NSError?*?error;?? ????/**根據(jù)請求的類型?以及?請求的url創(chuàng)建一個NSMutableURLRequest---通過該url去匹配請求隊列中是否有該url,如果有的話?那么就取消該請求*/?? ????NSString?*?urlToPeCanced?=?[[[[BJAppClient?sharedClient].requestSerializer?? ??????????????????????????????????requestWithMethod:requestType?URLString:string?parameters:nil?error:&error]?URL]?path];?? ?????? ????for?(NSOperation?*?operation?in?[BJAppClient?sharedClient].operationQueue.operations)?{?? ????????//如果是請求隊列?? ????????if?([operation?isKindOfClass:[NSURLSessionTask?class]])?{?? ????????????//請求的類型匹配?? ????????????BOOL?hasMatchRequestType?=?[requestType?isEqualToString:[[(NSURLSessionTask?*)operation?currentRequest]?HTTPMethod]];?? ????????????//請求的url匹配?? ????????????BOOL?hasMatchRequestUrlString?=?[urlToPeCanced?isEqualToString:[[[(NSURLSessionTask?*)operation?currentRequest]?URL]?path]];?? ????????????//兩項都匹配的話??取消該請求?? ????????????if?(hasMatchRequestType&&hasMatchRequestUrlString)?{?? ????????????????[operation?cancel];?? ????????????}?? ????????}?? ????}?? } ? ??
相關鏈接:
https://github.com/boai/BANetManager http://www.jianshu.com/p/6856bd9050fc http://blog.csdn.net/heberan/article/details/51567165 NSURLCache http://www.cnblogs.com/cbw1987/p/5910624.html
轉載于:https://www.cnblogs.com/xieyulin/p/7060529.html
總結
以上是生活随笔 為你收集整理的AFNetworking 3.1.0 使用中某些知识点讲解 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內容還不錯,歡迎將生活随笔 推薦給好友。