讀AFNetworking源碼

把AFNetworking的源碼讀了一遍,關(guān)于詳細的源碼解析的文章網(wǎng)上已經(jīng)有很多,便不再贅述。我嘗試從源碼里尋找以下問題的答案:

  • 用戶發(fā)起一次網(wǎng)絡(luò)請求后,AFN都做了些什么?
  • UIImageView+AFNetworking怎么實現(xiàn)圖片的下載與緩存?

先來個整體的架構(gòu):

整體架構(gòu)

最核心的兩個類是AFURLSessionManagerAFHTTPSessionManager,他們的職責簡單來說就是:

  • 提供一組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];

來個順序圖會比較清楚一些:

發(fā)起POST請求

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

主要做了:

  1. 做一些保護,判空,排重
  2. 起一個AFImageDownloader單例來完成真正的下載任務(wù)
  3. 先找cache,有直接返回,沒有的話用AFImageDownloader開啟下載
  4. 用一個叫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;
}
  1. NSURLRequestUseProtocolCachePolicy是默認緩存策略, 其策略如下:
    NSURLRequestUseProtocolCachePolicy decision tree for HTTP and HTTPS

    詳情見:https://developer.apple.com/reference/foundation/nsurlrequestcachepolicy
  2. allowsCellularAccess = YES,允許非wifi下下載圖片,這是當然
  3. URLCache允許20MB的內(nèi)存空間和150MB的磁盤空間。當應(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步:

  1. 如果是重復請求,將成功和失敗的block回調(diào)存入之前已存在的任務(wù)中
  2. 如果緩存策略允許,嘗試從緩存中取圖片
  3. 創(chuàng)建請求,response序列化對象
  4. 將成功失敗的block存起來備用
  5. 根據(jù)當前活躍請求數(shù),直接開啟下載任務(wù)或者加入下載隊列

可以看到,這些步驟都被放入了一個串行隊列synchronizationQueue中,并用dispatch_sync的方式調(diào)用,保證了線程安全性。
在圖片下載成功的回調(diào)中,將當前活躍任務(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不太好定制化需求,比如想更精準的控制緩存清理的時間用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;
}
  1. 每次命中緩存都會更新cache的最后訪問時間lastAccessDate
  2. 如果有新圖加入緩存了以后,緩存占用內(nèi)存超出了容量,會清理部分緩存的圖片
  3. 從最久沒被使用的圖開始清理,直到所占內(nèi)存達標

默認的內(nèi)存容量為100M,每次清理后默認會降到60M以下:

- (instancetype)init {
    return [self initWithMemoryCapacity:100 * 1024 * 1024 preferredMemoryCapacity:60 * 1024 * 1024];
}

大致先分析到這里。

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

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

  • 前不久做了一個生成快照的需求,其中用到 SDWebImage 來下載圖片,在使用該框架的過程中也遇到了一些問題,索...
    ShannonChenCHN閱讀 14,324評論 12 241
  • 圖片下載的這些回調(diào)信息存儲在SDWebImageDownloader類的URLOperations屬性中,該屬性是...
    怎樣m閱讀 2,688評論 0 1
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,704評論 4 61
  • 1.自定義控件 a.繼承某個控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,716評論 2 4
  • 2017.11.06 星期一 最近女兒開始學習琵琶彈音了,連續(xù)的練習總是因為自己無法彈出音或者不準而著急,進...
    蝸牛小于閱讀 446評論 4 1

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