iOS --- 使用NSOperation和NSURLSession封装一个串行下载器

本文介绍了使用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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容