把AFNetworking的源碼讀了一遍疯汁,關(guān)于詳細的源碼解析的文章網(wǎng)上已經(jīng)有很多娩践,便不再贅述三妈。我嘗試從源碼里尋找以下問題的答案:
- 用戶發(fā)起一次網(wǎng)絡(luò)請求后陪腌,AFN都做了些什么辱魁?
- UIImageView+AFNetworking怎么實現(xiàn)圖片的下載與緩存烟瞧?
先來個整體的架構(gòu):
最核心的兩個類是AFURLSessionManager
和AFHTTPSessionManager
,他們的職責(zé)簡單來說就是:
- 提供一組API給客戶端發(fā)起網(wǎng)絡(luò)請求
- 協(xié)調(diào)系統(tǒng)各個其他組件染簇,類似于控制器的角色
- 調(diào)用
AFHTTPRequestSerializer
加工網(wǎng)絡(luò)請求 - 調(diào)用
AFHTTPResponseSerializer
解析返回數(shù)據(jù) - 調(diào)用
AFNetworkReachabilityManager
判斷網(wǎng)絡(luò)狀態(tài) - 調(diào)用
AFSecurityPolicy
驗證HTTPS請求證書的有效性 - 內(nèi)部利用
AFURLSessionManagerTaskDelegate
處理部分NSURLSessionTaskDelegate
回調(diào)
用戶發(fā)起一次網(wǎng)絡(luò)請求后燕刻,AFN都做了些什么?
假設(shè)用戶發(fā)起一次POST請求:
[self.manager
POST:@"post"
parameters:@{@"key":@"value"}
progress:nil
success:nil
failure:nil];
來個順序圖會比較清楚一些:
UIImageView+AFNetworking怎么實現(xiàn)圖片的下載與緩存剖笙?
先來看下調(diào)setImageWithURLRequest:placeholderImage:success:failure:
后AFN都做了些什么卵洗,直接貼代碼了:
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
{
if ([urlRequest URL] == nil) {
[self cancelImageDownloadTask];
self.image = placeholderImage;
return;
}
if ([self isActiveTaskURLEqualToURLRequest:urlRequest]){
return;
}
[self cancelImageDownloadTask];
AFImageDownloader *downloader = [[self class] sharedImageDownloader];
id <AFImageRequestCache> imageCache = downloader.imageCache;
//Use the image from the image cache if it exists
UIImage *cachedImage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil];
if (cachedImage) {
if (success) {
success(urlRequest, nil, cachedImage);
} else {
self.image = cachedImage;
}
[self clearActiveDownloadInformation];
} else {
if (placeholderImage) {
self.image = placeholderImage;
}
__weak __typeof(self)weakSelf = self;
NSUUID *downloadID = [NSUUID UUID];
AFImageDownloadReceipt *receipt;
receipt = [downloader
downloadImageForURLRequest:urlRequest
withReceiptID:downloadID
success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) {
if (success) {
success(request, response, responseObject);
} else if(responseObject) {
strongSelf.image = responseObject;
}
[strongSelf clearActiveDownloadInformation];
}
}
failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) {
if (failure) {
failure(request, response, error);
}
[strongSelf clearActiveDownloadInformation];
}
}];
self.af_activeImageDownloadReceipt = receipt;
}
}
主要做了:
- 做一些保護犹撒,判空棵里,排重
- 起一個
AFImageDownloader
單例來完成真正的下載任務(wù) - 先找cache,有直接返回硬鞍,沒有的話用
AFImageDownloader
開啟下載 - 用一個叫
AFImageDownloadReceipt
的對象來記錄每一次下載聚至,可以理解為一個下載收據(jù)酷勺,用[NSUUID UUID]
作為它的ID,每個UIImageView
實例對應(yīng)了唯一個正在下載的收據(jù)扳躬,用af_activeImageDownloadReceipt
持有
這里要注意的是
AFImageDownloader
是個單例脆诉,AFN所有UIKit分類中需要用到下載圖片的地方都用了同一個downloader.
真正干活的是AFImageDownloader
,我們來看一下它的實現(xiàn)贷币。
先來看下初始化函數(shù):
- (instancetype)init {
NSURLSessionConfiguration *defaultConfiguration = [self.class defaultURLSessionConfiguration];
AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:defaultConfiguration];
sessionManager.responseSerializer = [AFImageResponseSerializer serializer];
return [self initWithSessionManager:sessionManager
downloadPrioritization:AFImageDownloadPrioritizationFIFO
maximumActiveDownloads:4
imageCache:[[AFAutoPurgingImageCache alloc] init]];
}
實例化了一個AFHTTPSessionManager
击胜,給它配了個sessionConfiguration和AFImageResponseSerializer
類型的responseSerializer,然后設(shè)置它的下載優(yōu)先級為FIFO(先來先下載役纹,合情合理)偶摔,最大同時下載數(shù)為4(這個必須得控制下),cache類型為AFAutoPurgingImageCache
促脉。
先來看下sessionConfiguration:
+ (NSURLCache *)defaultURLCache {
// It's been discovered that a crash will occur on certain versions
// of iOS if you customize the cache.
//
// More info can be found here: https://devforums.apple.com/message/1102182#1102182
//
// When iOS 7 support is dropped, this should be modified to use
// NSProcessInfo methods instead.
if ([[[UIDevice currentDevice] systemVersion] compare:@"8.2" options:NSNumericSearch] == NSOrderedAscending) {
return [NSURLCache sharedURLCache];
}
return [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
diskCapacity:150 * 1024 * 1024
diskPath:@"com.alamofire.imagedownloader"];
}
+ (NSURLSessionConfiguration *)defaultURLSessionConfiguration {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//TODO set the default HTTP headers
configuration.HTTPShouldSetCookies = YES;
configuration.HTTPShouldUsePipelining = NO;
configuration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
configuration.allowsCellularAccess = YES;
configuration.timeoutIntervalForRequest = 60.0;
configuration.URLCache = [AFImageDownloader defaultURLCache];
return configuration;
}
-
NSURLRequestUseProtocolCachePolicy
是默認(rèn)緩存策略辰斋, 其策略如下:
詳情見:https://developer.apple.com/reference/foundation/nsurlrequestcachepolicy -
allowsCellularAccess = YES
,允許非wifi下下載圖片瘸味,這是當(dāng)然 -
URLCache
允許20MB的內(nèi)存空間和150MB的磁盤空間宫仗。當(dāng)應(yīng)用不在前臺跑的時候,如果系統(tǒng)磁盤空間不夠了旁仿,磁盤上的圖片緩存會被清理藕夫。
切入正題,來看下圖片是怎么下載的:
- (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
withReceiptID:(nonnull NSUUID *)receiptID
success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success
failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure {
__block NSURLSessionDataTask *task = nil;
dispatch_sync(self.synchronizationQueue, ^{
NSString *URLIdentifier = request.URL.absoluteString;
if (URLIdentifier == nil) {
if (failure) {
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil];
dispatch_async(dispatch_get_main_queue(), ^{
failure(request, nil, error);
});
}
return;
}
// 1) Append the success and failure blocks to a pre-existing request if it already exists
AFImageDownloaderMergedTask *existingMergedTask = self.mergedTasks[URLIdentifier];
if (existingMergedTask != nil) {
AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID success:success failure:failure];
[existingMergedTask addResponseHandler:handler];
task = existingMergedTask.task;
return;
}
// 2) Attempt to load the image from the image cache if the cache policy allows it
switch (request.cachePolicy) {
case NSURLRequestUseProtocolCachePolicy:
case NSURLRequestReturnCacheDataElseLoad:
case NSURLRequestReturnCacheDataDontLoad: {
UIImage *cachedImage = [self.imageCache imageforRequest:request withAdditionalIdentifier:nil];
if (cachedImage != nil) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
success(request, nil, cachedImage);
});
}
return;
}
break;
}
default:
break;
}
// 3) Create the request and set up authentication, validation and response serialization
NSUUID *mergedTaskIdentifier = [NSUUID UUID];
NSURLSessionDataTask *createdTask;
__weak __typeof__(self) weakSelf = self;
createdTask = [self.sessionManager
dataTaskWithRequest:request
uploadProgress:nil
downloadProgress:nil
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
dispatch_async(self.responseQueue, ^{
__strong __typeof__(weakSelf) strongSelf = weakSelf;
AFImageDownloaderMergedTask *mergedTask = self.mergedTasks[URLIdentifier];
if ([mergedTask.identifier isEqual:mergedTaskIdentifier]) {
mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier];
if (error) {
for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
if (handler.failureBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
handler.failureBlock(request, (NSHTTPURLResponse*)response, error);
});
}
}
} else {
[strongSelf.imageCache addImage:responseObject forRequest:request withAdditionalIdentifier:nil];
for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
if (handler.successBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
handler.successBlock(request, (NSHTTPURLResponse*)response, responseObject);
});
}
}
}
}
[strongSelf safelyDecrementActiveTaskCount];
[strongSelf safelyStartNextTaskIfNecessary];
});
}];
// 4) Store the response handler for use when the request completes
AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID
success:success
failure:failure];
AFImageDownloaderMergedTask *mergedTask = [[AFImageDownloaderMergedTask alloc]
initWithURLIdentifier:URLIdentifier
identifier:mergedTaskIdentifier
task:createdTask];
[mergedTask addResponseHandler:handler];
self.mergedTasks[URLIdentifier] = mergedTask;
// 5) Either start the request or enqueue it depending on the current active request count
if ([self isActiveRequestCountBelowMaximumLimit]) {
[self startMergedTask:mergedTask];
} else {
[self enqueueMergedTask:mergedTask];
}
task = mergedTask.task;
});
if (task) {
return [[AFImageDownloadReceipt alloc] initWithReceiptID:receiptID task:task];
} else {
return nil;
}
}
作者注釋中已經(jīng)寫的很清楚了丁逝,分為5步:
- 如果是重復(fù)請求汁胆,將成功和失敗的block回調(diào)存入之前已存在的任務(wù)中
- 如果緩存策略允許,嘗試從緩存中取圖片
- 創(chuàng)建請求霜幼,response序列化對象
- 將成功失敗的block存起來備用
- 根據(jù)當(dāng)前活躍請求數(shù),直接開啟下載任務(wù)或者加入下載隊列
可以看到誉尖,這些步驟都被放入了一個串行隊列synchronizationQueue
中罪既,并用dispatch_sync
的方式調(diào)用,保證了線程安全性。
在圖片下載成功的回調(diào)中琢感,將當(dāng)前活躍任務(wù)數(shù)-1丢间,并在下載隊列里拿一個任務(wù)出來下載:
[strongSelf safelyDecrementActiveTaskCount];
[strongSelf safelyStartNextTaskIfNecessary];
接著來看下圖片的緩存,這里用了AFAutoPurgingImageCache
這個類來做驹针。
這是個實現(xiàn)了AFImageRequestCache
協(xié)議的類:
@protocol AFImageRequestCache <AFImageCache>
- (void)addImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
- (BOOL)removeImageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
- (nullable UIImage *)imageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
@end
如果它的緩存實現(xiàn)不滿足你的要求烘挫,也可以自己實現(xiàn)一套。
記得以前老版本的AFN用了NSCache來做圖片緩存柬甥,現(xiàn)在的版本在AFAutoPurgingImageCache
里用了NSDictionary自己實現(xiàn)了一套饮六,作者應(yīng)該是覺得系統(tǒng)的NSCache不太好定制化需求,比如想更精準(zhǔn)的控制緩存清理的時間用NSCache就做不到苛蒲。不過NSCache是線程安全的卤橄,用了NSDictionary必須自己來保證。代碼里用了dispatch_barrier_async
來保證線程安全:
- (void)addImage:(UIImage *)image withIdentifier:(NSString *)identifier {
dispatch_barrier_async(self.synchronizationQueue, ^{
AFCachedImage *cacheImage = [[AFCachedImage alloc] initWithImage:image identifier:identifier];
AFCachedImage *previousCachedImage = self.cachedImages[identifier];
if (previousCachedImage != nil) {
self.currentMemoryUsage -= previousCachedImage.totalBytes;
}
self.cachedImages[identifier] = cacheImage;
self.currentMemoryUsage += cacheImage.totalBytes;
});
dispatch_barrier_async(self.synchronizationQueue, ^{
if (self.currentMemoryUsage > self.memoryCapacity) {
UInt64 bytesToPurge = self.currentMemoryUsage - self.preferredMemoryUsageAfterPurge;
NSMutableArray <AFCachedImage*> *sortedImages = [NSMutableArray arrayWithArray:self.cachedImages.allValues];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastAccessDate"
ascending:YES];
[sortedImages sortUsingDescriptors:@[sortDescriptor]];
UInt64 bytesPurged = 0;
for (AFCachedImage *cachedImage in sortedImages) {
[self.cachedImages removeObjectForKey:cachedImage.identifier];
bytesPurged += cachedImage.totalBytes;
if (bytesPurged >= bytesToPurge) {
break ;
}
}
self.currentMemoryUsage -= bytesPurged;
}
});
}
- (nullable UIImage *)imageWithIdentifier:(NSString *)identifier {
__block UIImage *image = nil;
dispatch_sync(self.synchronizationQueue, ^{
AFCachedImage *cachedImage = self.cachedImages[identifier];
image = [cachedImage accessImage];
});
return image;
}
- (UIImage*)accessImage {
self.lastAccessDate = [NSDate date];
return self.image;
}
- 每次命中緩存都會更新cache的最后訪問時間
lastAccessDate
- 如果有新圖加入緩存了以后臂外,緩存占用內(nèi)存超出了容量窟扑,會清理部分緩存的圖片
- 從最久沒被使用的圖開始清理,直到所占內(nèi)存達標(biāo)
默認(rèn)的內(nèi)存容量為100M漏健,每次清理后默認(rèn)會降到60M以下:
- (instancetype)init {
return [self initWithMemoryCapacity:100 * 1024 * 1024 preferredMemoryCapacity:60 * 1024 * 1024];
}
大致先分析到這里嚎货。