gpt4 book ai didi

ios - 按优先顺序在后台下载许多文件(照片、视频)

转载 作者:IT王子 更新时间:2023-10-29 05:43:49 24 4
gpt4 key购买 nike

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

我正在使用 Alamofire 并设置了 session 管理器:

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

然后我这样使用它:

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

它在 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)
}

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

我正在查看 alamofire,它具有组件库 AlamofireImage,它具有基于优先级的下载,但图像只是我想要确定优先级的文件的一部分。感谢帮助

最佳答案

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

你需要做的是:

1.HTTPMaximumConnectionsPerHost 设置为 1 以确保下载按顺序进行:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = 30.0;
configuration.HTTPMaximumConnectionsPerHost = 1; // Note this

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

-(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];
}
}

Don't forget to enable background mode: enableBackgroundMode:YES

3. 在 Xcode 中启用后台模式:

enter image description here

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

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

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

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

关于ios - 按优先顺序在后台下载许多文件(照片、视频),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40912751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com