REDownloadTasksQueue 1.1.0

REDownloadTasksQueue 1.1.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2016年9月

Oleh Kulykov 维护。



 
依赖于
NSMutableNumber>= 0
Inlineobjc>= 0
 

  • 作者:'
  • Oleh Kulykov

基于 NSURLSessionDownloadTask 的 iOS Objective C 下载队列。

主要功能

  • 使用 NSURLSessionDownloadTask,要求 iOS 7 和以上。
  • 使用另一个操作队列进行下载。
  • 控制并发任务进行并行下载,可以根据网络连接质量进行调整。
  • 可以通过代理、块和通知来通知队列状态。可以选择使用哪种方法。
  • 每个任务基于已下载数据量进行进度计算,而不是任务的数量,以实现平滑的进度。
  • 队列可以序列化/反序列化以用于将来重用。
  • 需要 ARC。

Podfile

platform :ios, '7.0'
pod 'REDownloadTasksQueue'

创建并填充队列 URL

#import "REDownloadTasksQueue.h" // include single queue header file

self.queue = [[REDownloadTasksQueue alloc] init]; // create and store strongly queue object
for (...) // iterate URL's
{
    NSString * fromURLString = ...; // URL string for download file
    NSString * storePath = ...; // Full path for storing downloaded file data
    [_queue addURLString:fromURLString withStorePath:storePath]; // add as URL string
    [_queue addURL:[NSURL URLWithString:fromURLString] withStorePath:storePath]; // add as URL
}
[_queue start]; // start queue

通过块跟踪队列事件

[_queue setOnErrorOccurredHandler:^(REDownloadTasksQueue * queue, NSError * error, NSURL * downloadURL, NSURL * storeFilePathURL){
    NSLog(@"onErrorOccurred, error: %@, from: %@, to: %@", error, downloadURL, storeFilePathURL);
}];

[_queue setOnFinishedHandler:^(REDownloadTasksQueue * queue){
    NSLog(@"onFinished");
}];

[_queue setOnProgressHandler:^(REDownloadTasksQueue * queue, float progress){
    NSLog(@"onProgress, progress: %f %%", progress);
}];
[_queue start];

通过通知跟踪队列事件

#pragma mark - REDownloadTasksQueue notifications
- (void) onOnDownloadTasksQueueErrorOccurredNotification:(NSNotification *) notification
{
    NSDictionary * userInfo = [notification userInfo];
    REDownloadTasksQueue * queue = [userInfo objectForKey:kREDownloadTasksQueueQueueKey];
    id userObject = [userInfo objectForKey:kREDownloadTasksQueueUserObjectKey];
    NSError * error = [userInfo objectForKey:kREDownloadTasksQueueErrorKey];
    NSURL * downloadURL = [userInfo objectForKey:kREDownloadTasksQueueDownloadURLKey];
    NSURL * storeURL = [userInfo objectForKey:kREDownloadTasksQueueStoreURLKey];
    // Process error
}

- (void) onOnDownloadTasksQueueFinishedNotification:(NSNotification *) notification
{
    NSDictionary * userInfo = [notification userInfo];
    REDownloadTasksQueue * queue = [userInfo objectForKey:kREDownloadTasksQueueQueueKey];
    id userObject = [userInfo objectForKey:kREDownloadTasksQueueUserObjectKey];
    // Process finished situation
}

- (void) onOnDownloadTasksQueueProgressChangedNotification:(NSNotification *) notification
{
    NSDictionary * userInfo = [notification userInfo];
    REDownloadTasksQueue * queue = [userInfo objectForKey:kREDownloadTasksQueueQueueKey];
    id userObject = [userInfo objectForKey:kREDownloadTasksQueueUserObjectKey];
    NSNumber * progressNumber = [userInfo objectForKey:kREDownloadTasksQueueProgressKey];
    NSLog(@"onProgress, progress: %f %%", [progressNumber floatValue]);
    // Process progressing
}

// setup queue observing
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(onOnDownloadTasksQueueErrorOccurredNotification:)
                                             name:kREDownloadTasksQueueErrorNotification
                                           object:nil /* or queue object */];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(onOnDownloadTasksQueueFinishedNotification:)
                                             name:kREDownloadTasksQueueDidFinishedNotification
                                           object:nil /* or queue object */];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(onOnDownloadTasksQueueProgressChangedNotification:)
                                             name:kREDownloadTasksQueueProgressChangedNotification
                                           object:nil /* or queue object */];

[_queue start];

通过代理跟踪队列事件

#pragma mark - REDownloadTasksQueueDelegate
- (void) onREDownloadTasksQueueFinished:(REDownloadTasksQueue *) queue
{
    // Process finished
}

- (void) onREDownloadTasksQueue:(REDownloadTasksQueue *) queue 
                       progress:(float) progress
{
    NSLog(@"onProgress, progress: %f %%", progress);
    // Process progressing
}

- (void) onREDownloadTasksQueue:(REDownloadTasksQueue *) queue 
                          error:(NSError *) error 
                    downloadURL:(NSURL *) downloadURL 
                       storeURL:(NSURL *) storeURL
{
    // Process error
}

// setup queue delegate
[_queue setDelegate:self];
[_queue start];

序列化队列以供将来重用

__weak SomeClassWhichHoldsQueue * weakSelf = self;
[_queue cancelAndSerializeWithRestorationID:^(NSString * restorationID){
            NSLog(@"Stored restorationID: %@", restorationID);
            weakSelf.restorationID = restorationID;
        }];

使用恢复标识符反序列化队列,例如:在应用程序重启后

__weak SomeClassWhichHoldsQueue * weakSelf = self;
[REDownloadTasksQueue createWithRestorationID:weakSelf.restorationID 
                         andCompletionHandler:^(REDownloadTasksQueue * restoredQueue, NSError * error){
                             NSLog(@"Restored");
                             weakSelf.queue = restoredQueue;
                             [restoredQueue start];
                         }];

许可证


MIT 许可证 (MIT)

版权所有 (c) 2014 - 2016 Kulykov Oleh [email protected]

本许可协议已授予任何获得本软件及其相关文档副本(“软件”)的人免费许可权,允许不受限制地使用软件,包括但不限于使用、复制、修改、合并、发布、分发、转授许可并/或出售软件副本,并且允许拥有软件的人许可他人以本许可协议的限制方式使用。

上述版权声明和本许可协议应当在软件的所有副本或主要部分中体现。

本软件按“原样”提供,不提供任何形式的保证,无论是明示的还是默示的,包括但不限于适销性、适用于特定目的和侵权不保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担任何责任,无论这些责任是由合同、侵权或其他法律关系引起的,无论这些责任与该软件或该软件的使用或其他交易有关。