使用NSOperation和NSURLSession封裝一個序列下載器

克里斯_萊昂納多_羅斯柴爾德發表於2018-06-07

本文介紹了使用NSOperation和NSURLSession來實現序列下載的需求.

為何要這樣

iOS中使用NSURLSession的NSURLSessionDownloadTask進行下載: 對於NSURLSessionDownloadTask物件, 執行resume方法之後, 即開始下載任務. 而下載進度是通過NSURLSessionDelegate的對應方法進行更新. 這意味著在發起下載任務後, 實際的下載操作是非同步執行的. 如果順序發起多個下載任務(執行resume方法), 各個任務的下載情況完全是在NSURLSessionDelegate的回撥方法中體現. 這樣會出現幾個問題:

  • 多工同時下載: 在iOS上NSURLSession允許4個任務同時下載,在一些應用體驗上其實不如單個順序下載(如音樂下載, 相機AR素材包下載等, 與其多首歌曲同時下載, 不如優先下載完一首, 使用者可以儘快使用).
  • 任務間有依賴關係: 如AR素材包本身下載完成之後, 還要依賴另外的一個配置檔案(Config.zip)等下載完成, 則即使該AR素材包下載完成, 但依然無法使用, 不能置為已下載狀態.
  • 優先順序問題: 如有的任務的優先順序比較高, 則需要做到優先下載.
  • 下載完成時間不確定: 如上的使用場景, 因AR素材包和依賴檔案的下載完成順序也不確定, 導致必須採用一些機制去觸發全部下載完畢的後續操作(如通知等).
  • 下載超時: NSURLSessionDownloadTask物件執行resume後, 如果在指定時間內未能下載完畢會出現下載超時, 多個任務同時下載時容易出現.

目標

以上邊講的AR素材包的場景為例, 我們想要實現一個下載機制:

  • 順序點選多個AR素材, 發起多個下載請求, 但優先下載一個素材包, 以便使用者可以儘快體驗效果.
  • 對於有依賴關係的素材包, 先下載其依賴的配置檔案, 再下載素材包本身, 素材包本身的下載完成狀態即是該AR整體的下載完成狀態.

實現過程

綜合以上的需求, 使用NSOperation來封裝下載任務, 但需要監控其狀態. 使用NSOperationQueue來管理這些下載任務.

NSOperation的使用

CSDownloadOperation繼承自NSOperation, 不過對於其executing, finished, cancelled狀態, 需要使用KVO監控.

因為KVO依賴於屬性的setter方法, 而NSOperation的這三個屬性是readonly的, 所以NSOperation在執行中的這些狀態變化不會自動觸發KVO, 而是需要我們額外做一些工作來手動觸發KVO.

其實, 可以簡單理解為給NSOperation的這三個屬性自定義setter方法, 以便在其狀態變化時觸發KVO.

@interface CSDownloadOperation : NSOperation

@end

@interface CSDownloadOperation ()

// 因這些屬性是readonly, 不會自動觸發KVO. 需要手動觸發KVO, 見setter方法.
@property (assign, nonatomic, getter = isExecuting)     BOOL executing;
@property (assign, nonatomic, getter = isFinished)      BOOL finished;
@property (assign, nonatomic, getter = isCancelled)     BOOL cancelled;

@end

@implementation CSDownloadOperation

@synthesize executing       = _executing;
@synthesize finished        = _finished;
@synthesize cancelled       = _cancelled;


- (void)setExecuting:(BOOL)executing
{
    [self willChangeValueForKey:@"isExecuting"];
    _executing = executing;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)setFinished:(BOOL)finished
{
    [self willChangeValueForKey:@"isFinished"];
    _finished = finished;
    [self didChangeValueForKey:@"isFinished"];
}

- (void)setCancelled:(BOOL)cancelled
{
    [self willChangeValueForKey:@"isCancelled"];
    _cancelled = cancelled;
    [self didChangeValueForKey:@"isCancelled"];
}

@end
複製程式碼

NSOperation執行時, 發起NSURLSessionDownloadTask的下載任務(執行resume方法), 然後等待該任務下載完成, 才去更新NSOperation的下載完成狀態. 然後NSOperationQueue才能發起下一個任務的下載.

在初始化方法中, 構建好NSURLSessionDownloadTask物件, 及下載所需的一些配置等.

- (void)p_setupDownload {
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    self.urlSession = [NSURLSession sessionWithConfiguration:config
                                                    delegate:self
                                               delegateQueue:[NSOperationQueue mainQueue]];

    NSURL *url = [NSURL URLWithString:self.downloadItem.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                         timeoutInterval:kTimeoutIntervalDownloadOperation];
    self.downloadTask = [self.urlSession downloadTaskWithRequest:request];
    self.downloadTask.taskDescription = self.downloadItem.urlString;
}
複製程式碼

重寫其start, main和cancel方法:

/**
 必須重寫start方法.
 若不重寫start, 則cancel掉一個op, 會導致queue一直卡住.
 */
- (void)start
{
//    NSLog(@"%s %@", __func__, self);

    // 必須設定finished為YES, 不然也會卡住
    if ([self p_checkCancelled]) {
        return;
    }

    self.executing  = YES;

    [self main];
}

- (void)main
{
    if ([self p_checkCancelled]) {
        return;
    }

    [self p_startDownload];

    while (self.executing) {
        if ([self p_checkCancelled]) {
            return;
        }
    }
}

- (void)cancel
{
    [super cancel];

    [self p_didCancel];
}

複製程式碼

在p_startDownload方法中發起下載:

- (void)p_startDownload
{
    [self.downloadTask resume];
}
複製程式碼

使用NSURLSessionDownloadDelegate來更新下載狀態

實現該協議的回撥方法, 更新下載進度, 下載完成時更新狀態.

- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
    // xxx
    [self p_done];
    // xxx
}

/* Sent periodically to notify the delegate of download progress. */
- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    CGFloat progress = 1.0 * totalBytesWritten / totalBytesExpectedToWrite;
    // xxx
    // 更新下載進度等
    // xxx
}

- (void)p_done
{
//    NSLog(@"%s %@", __func__, self);

    [self.urlSession finishTasksAndInvalidate];
    self.urlSession = nil;

    self.executing  = NO;
    self.finished   = YES;
}
複製程式碼

使用NSOperationQueue來管理序列下載佇列

NSOperation中發起下載之後, 並不會立即設定其finished為YES, 而是會有一個while迴圈, 一直等到NSURLSessionDownloadDelegate的回撥方法執行, 才會更新其finished狀態.

而NSOperationQueue的特點就是上一個NSOperation的finished狀態未置為YES, 不會開始下一個NSOperation的執行.

設定優先順序

對NSOperation的優先順序進行設定即可.

CSDownloadOperationQueue *queue = [CSDownloadOperationQueue sharedInstance];
CSDownloadOperation *op = [[CSDownloadOperation alloc] initWithDownloadItem:downloadItem
                                                               onOperationQueue:queue];
op.downloadDelegate = self;

// AR背景的優先順序提升
op.queuePriority = NSOperationQueuePriorityHigh;
複製程式碼

獲取下載進度及下載完成狀態

通過實現CSDownloadOperationQueueDelegate, 以觀察者的身份來接收下載進度及下載完成狀態.

// MARK: - CSDownloadOperationQueueDelegate

/**
 CSDownloadOperationQueueDelegate通知obsever來更新下載進度
 */
@protocol CSDownloadOperationQueueDelegate <NSObject>

@optional
- (void)CSDownloadOperationQueue:(CSDownloadOperationQueue *)operationQueue
               downloadOperation:(CSDownloadOperation *)operation
             downloadingProgress:(CGFloat)progress;

- (void)CSDownloadOperationQueue:(CSDownloadOperationQueue *)operationQueue
               downloadOperation:(CSDownloadOperation *)operation
                downloadFinished:(BOOL)isSuccessful;

@end
複製程式碼

注意這裡觀察者模式的使用: observer為繼承delegate的物件, 記憶體管理語義當然為weak.

// MARK: - observer

/**
 use observer to notify the downloading progress and result
 */
- (void)addObserver:(id<CSDownloadOperationQueueDelegate>)observer;
- (void)removeObserver:(id<CSDownloadOperationQueueDelegate>)observer;
複製程式碼

所以, 需要使用NSValue的nonretainedObjectValue. 除此之外, 可以使用NSPointerArray來實現弱引用物件的容器.

- (NSMutableArray <NSValue *> *)observers {
    if (!_observers) {
        _observers = [NSMutableArray array];
    }

    return _observers;
}

- (void)addObserver:(id<CSDownloadOperationQueueDelegate>)observer {
    @synchronized (self.observers) {
        BOOL isExisting = NO;

        for (NSValue *value in self.observers) {
            if ([value.nonretainedObjectValue isEqual:observer]) {
                isExisting = YES;
                break;
            }
        }

        if (!isExisting) {
            [self.observers addObject:[NSValue valueWithNonretainedObject:observer]];
            NSLog(@"@");
        }
    }
}

- (void)removeObserver:(id<CSDownloadOperationQueueDelegate>)observer {
    @synchronized (self.observers) {
        NSValue *existingValue = nil;

        for (NSValue *value in self.observers) {
            if ([value.nonretainedObjectValue isEqual:observer]) {
                existingValue = value;
                break;
            }
        }

        if (existingValue) {
            [self.observers removeObject:existingValue];
        }
    }
}
複製程式碼

Demo地址

CSSerialDownloader

相關文章