EasyDownloadSession 2.0.3

EasyDownloadSession 2.0.3

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016 年 6 月
SPM支持 SPM

Javier Laguna 维护。



  • Javier Laguna

Readme Score

EasyDownloadSession 允许暂停和恢复下载,让开发者完全控制任务执行。

使用方法

EasyDownload Session 基于 NSURLSession 构建,包含对一个会话对象的包装,该对象将为您安排新的下载任务。

它可以配置为运行所需数量的并发操作,但默认行为是 1 个运行任务。需要注意的是,这个 Pod 也适用于上传,但为了方便起见,我们将所有传输称为下载。

重要提示:每个主机的最大下载数量为 100。

缓存已禁用。

添加新下载有两种方式

  • scheduleDownloadWithId: 它将任务添加到堆栈中,并在有空闲的下载槽时运行。

  • forceDownloadWithId: 它将立即启动任务,并在必要时暂停其他下载。

我们可以在这个代码中使用这个代码的一个很好的例子:[email protected]:lagubull/SocialChallenge.git

我们在这个仓库中使用 EasyDownload Session 来添加下载大型图像的请求,这些图像将在之后被处理并在表视图中显示,因此我们使用此 Pod 提供的堆栈,因为我们更感兴趣于新的请求而不是旧的请求。

堆栈

EasyDownloadSession 可以管理所需的所有堆栈数量,在安排任何下载之前,您应该创建堆栈并将其注册到会话中。

        let mediaStack = Stack()

        DownloadSession.sharedInstance.registerStack(stack: mediaStack,
                                                     stackIdentifier:kJSCMediaDownloadStack)

安排

我们将请求添加到堆栈中,并遵循当前的下载任务。

下载成功完成后,将触发成功块。如果下载失败,将触发失败块。此外,将调用进度块以通知任务进度。

class func retrieveMediaForPost(post: JSCPost, retrievalRequired: ((postId: String) -> Void)?, success: ((result: AnyObject?, postId: String) -> Void)?, failure: ((error: NSError?, postId: String) -> Void)?) {

        let mySuccess = success
        let myFailure = failure

        if post.userAvatarRemoteURL != nil {

            let operation = JSCLocalImageAssetRetrievalOperation.init(postId: post.postId!)

            operation.onCompletion = { JSCOperationOnCompletionCallback in

                if let imageMedia = JSCOperationOnCompletionCallback {

                    mySuccess?(result: imageMedia,
                               postId: post.postId!)
                }
                else {

                    retrievalRequired?(postId: post.postId!)

                    DownloadSession.scheduleDownloadWithId(post.postId!,
                                                           fromURL: NSURL.init(string: post.userAvatarRemoteURL!)!,
                                                           stackIdentifier: kJSCMediaDownloadStack,
                                                           progress: nil,
                                                           success: { (taskInfo: DownloadTaskInfo!, responseData: NSData?) -> Void in

                                                            let storeOperation = JSCMediaStorageOperation.init(postId: post.postId!, data: responseData)

                                                            storeOperation.onSuccess = { JSCOperationOnSuccessCallback in

                                                                if let imageMedia = JSCOperationOnSuccessCallback {

                                                                    mySuccess?(result: imageMedia, postId: post.postId!)
                                                                }
                                                                else {

                                                                    myFailure?(error: nil, postId: post.postId!)
                                                                }
                                                            }

                                                            storeOperation.onFailure = { JSCOperationOnFailureCallback in

                                                                myFailure?(error: nil, postId: post.postId!)
                                                            }

                                                            storeOperation.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier

                                                            JSCOperationCoordinator.sharedInstance.addOperation(storeOperation)
                        },
                                                           failure: { (taskInfo, error) -> Void in

                                                            myFailure?(error: error, postId: post.postId!)
                    })
                }
            }

            operation.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier

            JSCOperationCoordinator.sharedInstance.addOperation(operation)
        }
        else {

            myFailure?(error: nil,
                       postId: post.postId!)
        }
    }
#pragma mark - Retrieval

+ (void)retrieveMediaForPost:(JSCPost *)post
           retrievalRequired:(void (^)(NSString *postId))retrievalRequired
                     success:(void (^)(id result, NSString *postId))success
                     failure:(void (^)(NSError *error, NSString *postId))failure;
{
 [EDSDownloadSession scheduleDownloadWithId:post.postId
                                    fromURL:[NSURL URLWithString:post.userAvatarRemoteURL]
                                    progress:nil
                                     success:^(EDSDownloadTaskInfo *downloadTask,
                                               NSData *responseData)
                 {
                     JSCMediaStorageOperation *storeOPeration =
                      [[JSCMediaStorageOperation alloc] initWithPostID:post.postId
                                                                  data:responseData];

                     storeOPeration.onSuccess = ^(id result)
                     {
                         if (result)
                         {
                             if (success)
                             {
                                 success(result, post.postId);
                             }
                         }
                         else
                         {
                             if (failure)
                             {
                                 failure(nil, post.postId);
                             }
                         }
                     };

                     storeOPeration.onFailure = ^(NSError *error)
                     {
                         if (failure)
                         {
                             failure(error, post.postId);
                         }
                     };

                     storeOPeration.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier;

                     [[JSCOperationCoordinator sharedInstance] addOperation:storeOPeration];
                 }
                                                   failure:^(EDSDownloadTaskInfo *downloadTask, NSError *error)
                 {
                     if (failure)
                     {
                         failure(error, post.postId);
                     }
                 }];
}

对于单一完成块的替代签名也可用

  DownloadSession.scheduleDownloadWithId(post.postId!,
                                                           fromURL: NSURL.init(string: post.userAvatarRemoteURL!)!,
                                                           stackIdentifier: kJSCMediaDownloadStack,
                                                           progress: nil,
                                                           completion: { (taskInfo: DownloadTaskInfo!, responseData: NSData?, error: NSError?) -> Void in

                                                            if error == nil {

                                                                let storeOperation = JSCMediaStorageOperation.init(postId: post.postId!, data: responseData)

                                                                storeOperation.onSuccess = { JSCOperationOnSuccessCallback in

                                                                    if let imageMedia = JSCOperationOnSuccessCallback {

                                                                        mySuccess?(result: imageMedia, postId: post.postId!)
                                                                    }
                                                                    else {

                                                                        myFailure?(error: nil, postId: post.postId!)
                                                                    }
                                                                }

                                                                storeOperation.onFailure = { JSCOperationOnFailureCallback in

                                                                    myFailure?(error: nil, postId: post.postId!)
                                                                }

                                                                storeOperation.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier

                                                                JSCOperationCoordinator.sharedInstance.addOperation(storeOperation)
                                                            }
                                                            else {

                                                                myFailure?(error: error, postId: post.postId!)
                                                            }
                    })
                }
            }

强制下载

这将有效地暂停所有下载,启动此下载,然后开始最多 maxDownloads 的任何其他下载。

下载成功完成后,将触发成功块。如果下载失败,将触发失败块。

此外,将调用进度块以通知任务进度。

class func retrieveMediaForPost(post: JSCPost, retrievalRequired: ((postId: String) -> Void)?, success: ((result: AnyObject?, postId: String) -> Void)?, failure: ((error: NSError?, postId: String) -> Void)?) {

        let mySuccess = success
        let myFailure = failure

        if post.userAvatarRemoteURL != nil {

            let operation = JSCLocalImageAssetRetrievalOperation.init(postId: post.postId!)

            operation.onCompletion = { JSCOperationOnCompletionCallback in

                if let imageMedia = JSCOperationOnCompletionCallback {

                    mySuccess?(result: imageMedia,
                               postId: post.postId!)
                }
                else {

                    retrievalRequired?(postId: post.postId!)

                    DownloadSession.forceDownloadWithId(post.postId!,
                                                        fromURL: NSURL.init(string: post.userAvatarRemoteURL!)!,
                                                        stackIdentifier: kJSCMediaDownloadStack,
                                                        progress: nil,
                                                        success: { (taskInfo: DownloadTaskInfo!, responseData: NSData?) -> Void in

                                                            let storeOperation = JSCMediaStorageOperation.init(postId: post.postId!, data: responseData)

                                                            storeOperation.onSuccess = { JSCOperationOnSuccessCallback in

                                                                if let imageMedia = JSCOperationOnSuccessCallback {

                                                                    mySuccess?(result: imageMedia, postId: post.postId!)
                                                                }
                                                                else {

                                                                    myFailure?(error: nil, postId: post.postId!)
                                                                }
                                                            }

                                                            storeOperation.onFailure = { JSCOperationOnFailureCallback in

                                                                myFailure?(error: nil, postId: post.postId!)
                                                            }

                                                            storeOperation.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier

                                                            JSCOperationCoordinator.sharedInstance.addOperation(storeOperation)
                        },
                                                        failure: { (taskInfo, error) -> Void in

                                                            myFailure?(error: error, postId: post.postId!)
                    })
                }
            }

            operation.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier

            JSCOperationCoordinator.sharedInstance.addOperation(operation)
        }
        else {

            myFailure?(error: nil,
                       postId: post.postId!)
        }
    }
#pragma mark - Retrieval

+ (void)retrieveMediaForPost:(JSCPost *)post
           retrievalRequired:(void (^)(NSString *postId))retrievalRequired
                     success:(void (^)(id result, NSString *postId))success
                     failure:(void (^)(NSError *error, NSString *postId))failure;
{
[EDSDownloadSession forceDownloadWithId:post.postId
                                fromURL:[NSURL URLWithString:post.userAvatarRemoteURL]
                               progress:nil
                                success:^(EDSDownloadTaskInfo *downloadTask,
                                               NSData *responseData)
                 {
                     JSCMediaStorageOperation *storeOPeration =
                      [[JSCMediaStorageOperation alloc] initWithPostID:post.postId
                                                                  data:responseData];

                     storeOPeration.onSuccess = ^(id result)
                     {
                         if (result)
                         {
                             if (success)
                             {
                                 success(result, post.postId);
                             }
                         }
                         else
                         {
                             if (failure)
                             {
                                 failure(nil, post.postId);
                             }
                         }
                     };

                     storeOPeration.onFailure = ^(NSError *error)
                     {
                         if (failure)
                         {
                             failure(error, post.postId);
                         }
                     };

                     storeOPeration.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier;

                     [[JSCOperationCoordinator sharedInstance] addOperation:storeOPeration];
                 }
                                                   failure:^(EDSDownloadTaskInfo *downloadTask, NSError *error)
                 {
                     if (failure)
                     {
                         failure(error, post.postId);
                     }
                 }];
}

对于单一完成块的替代签名也可用

DownloadSession.forceDownloadWithId(post.postId!,
                                                        fromURL: NSURL.init(string: post.userAvatarRemoteURL!)!,
                                                        stackIdentifier: kJSCMediaDownloadStack,
                                                        progress: nil,
                                                        completion: { (taskInfo: DownloadTaskInfo!, responseData: NSData?, error: NSError?) -> Void in

                                                            if error == nil {

                                                                let storeOperation = JSCMediaStorageOperation.init(postId: post.postId!, data: responseData)

                                                                storeOperation.onSuccess = { JSCOperationOnSuccessCallback in

                                                                    if let imageMedia = JSCOperationOnSuccessCallback {

                                                                        mySuccess?(result: imageMedia, postId: post.postId!)
                                                                    }
                                                                    else {

                                                                        myFailure?(error: nil, postId: post.postId!)
                                                                    }
                                                                }

                                                                storeOperation.onFailure = { JSCOperationOnFailureCallback in

                                                                    myFailure?(error: nil, postId: post.postId!)
                                                                }

                                                                storeOperation.targetSchedulerIdentifier = kJSCLocalDataOperationSchedulerTypeIdentifier

                                                                JSCOperationCoordinator.sharedInstance.addOperation(storeOperation)
                                                            }
                                                            else {

                                                                myFailure?(error: error, postId: post.postId!)
                                                            }
                    })
                }
            }

并发下载

您可以使用maxDownloads属性来增加并发下载的数量。

    mediaStack.maxDownloads = 4
    [EDSDownloadSession downloadSession].maxDownloads = @(4);

暂停

暂停下载会停止任务并在内存中存储进度,以便稍后恢复。

任何时候都可以暂停所有栈。

    DownloadSession.pauseDownloads()

同样可以很容易地停止特定的栈。

    DownloadSession.pauseDownloadsInStack(kJSCMediaDownloadStack)

恢复

手动暂停下载后,可以将其恢复,并从停止的地方继续。

要重启所有下载(在建立的限制内),你可以调用

    DownloadSession.resumeDownloads()

然而,要恢复特定栈

    DownloadSession.resumeDownloadsInStack(kJSCMediaDownloadStack)

取消

任何时间都可以取消所有任务。

    DownloadSession.cancelDownloads()

同样可以停止特定的任务。

     DownloadSession.cancelDownload(post.postId!,
                                    stackIdentifier: kJSCMediaDownloadStack)

发现了一个问题?

如果在EasyAlert中遇到特定问题,有功能需求或想要分享评论,请在此处新建问题

鼓励提出拉取请求,并将非常感谢!请尽量与现有的编码风格保持一致。如果您正在考虑承担项目的重要更改或添加,请通过提前打开新问题进行沟通。这可以让每个人都能了解即将到来的更改,确保更改与项目的设计理念保持一致,并且避免重复工作。

谢谢!