以优先顺序下载背景中的许多文件(照片,视频)

Lib*_*tal 9 background ios nsurlsessiondownloadtask swift alamofire

在应用程序的第一次启动我想从服务器下载所有文件,我想继续下载,即使用户离开应用程序(它不在前台).我需要下载的文件是缩略图,原始大小的照片,其他文件和视频.我想按照我之前写的顺序下载它们.

我正在使用Alamofire并设置会话管理器:

let backgroundManager: Alamofire.SessionManager = {
    let bundleIdentifier = "com....."
    return Alamofire.SessionManager(
      configuration: URLSessionConfiguration.background(withIdentifier: bundleIdentifier + ".background")
    )
  }()
Run Code Online (Sandbox Code Playgroud)

然后我像这样使用它:

  self.backgroundManager.download(fileUrl, to: destination)
    .downloadProgress { progress in
      //print("Download Progress: \(progress.fractionCompleted)")
    }
    .response(completionHandler: result)
Run Code Online (Sandbox Code Playgroud)

它在downloadPhoto方法中,我称之为:

for item in items {
      self.downloadPhoto(item: item, isThumbnail: true, shouldReloadData: false, indexPath: nil)
      self.downloadPhoto(item: item, isThumbnail: false, shouldReloadData: false, indexPath: nil)
}
Run Code Online (Sandbox Code Playgroud)

然后我可以添加文件下载和视频下载等呼叫.但所有这些请求具有相同的优先级,我想首先下载缩略图(因为这是用户最初看到的)然后下载全尺寸图像,然后下载所有图像,然后下载文件和视频.但所有必须在队列中,因为如果用户启动应用程序,然后将其设置为后台并将其保留几个小时,则必须全部下载.这可能吗?我怎么能这样做?

我正在寻找alamofire,它有组件库AlamofireImage,它具有基于优先级的下载,但图像只是我想要优先考虑的文件的一部分.感谢帮助

NSP*_*tik 4

看看TWRDDownloadManager。它使用NSURLSessionDownloadTask并支持后台模式。

你需要做的是:

1.设置HTTPMaximumConnectionsPerHost为 1 以确保下载连续进行:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = 30.0;
configuration.HTTPMaximumConnectionsPerHost = 1; // Note this
Run Code Online (Sandbox Code Playgroud)

2.下面是迭代for循环并逐一下载媒体的方法:

-(void)downloadDocuments
{
    for(int i=0; i<[arrDownloadList count]; i++)
    {
        Downloads *download = [arrDownloadList objectAtIndex:i];
        NSString *fileURL = download.documentURL;

        [[TWRDownloadManager sharedManager] downloadFileForURL:fileURL 
                                                      withName:[fileURL lastPathComponent] 
                                              inDirectoryNamed:kPATH_DOC_DIR_CACHE_DOCUMENTS
                                               completionBlock:^(BOOL completed)
         {
             if (completed) 
             {
                 /* To some task like database download flag updation or whatever you wanr */

                 downloadIndex++;

                 if(downloadIndex < [arrDownloadList count]) {
                     [self updateDownloadingStatus];
                 }
                 else {
                     [self allDownloadCompletedWithStatus:TRUE];
                 }
             }

             else
             {
                 /* Cancel the download */
                 [[TWRDownloadManager sharedManager] cancelDownloadForUrl:fileURL];

                 downloadIndex++;

                 if(downloadIndex < [arrDownloadList count]) {
                     [self updateDownloadingStatus];
                 }
                 else {
                     [self allDownloadCompletedWithStatus:TRUE];
                 }
             }

         } enableBackgroundMode:YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记启用后台模式:enableBackgroundMode:YES

3.在 Xcode 中启用Background Modes

在此输入图像描述

4.将以下方法添加到您的AppDelegate

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler{
    [TWRDownloadManager sharedManager].backgroundTransferCompletionHandler = completionHandler;   
}
Run Code Online (Sandbox Code Playgroud)

如果您这样做,下载将连续进行,即使应用程序在后台或用户锁定设备,下载也会继续。

如果对此有任何疑问或帮助,请添加评论。