gpt4 book ai didi

ios - GCD : Making Sequential Download Calls; ONE BY ONE

转载 作者:行者123 更新时间:2023-11-29 11:49:46 25 4
gpt4 key购买 nike

场景

该应用程序会逐个下载用户订阅。此调用将在多个位置进行(在另一个网络调用之后的完成 block 中以及从 UIAlertController 按下按钮)。逻辑是下载所有订阅,一旦一个订阅下载失败,它就会转到下一个,直到所有订阅都下载完毕,然后我们的 SVProgressHUD 关闭。当我们从 Xcode 构建和运行代码时,代码运行良好。但是当我们构建 IPA 并发送给我们的客户时,此逻辑会造成某种停顿,并且 SVProgressHUD 警报会一直旋转:

enter image description here

这是一个大问题,因为我们的应用程序专注于从订阅中下载内容。

为什么这个逻辑在我存档并从中构建 IPA 后停止,但在我从 Xcode 构建和运行时没有

代码如下:

// Making the call 

DispatchQueue.main.async {
DGWebService().syncUserSubscribedContent {
DispatchQueue.main.async {
self.finishLogin()
}
}
}

// Sequentially going through each subscription and downloading them

func syncUserSubscribedContent(completion: @escaping Constants.WebService.ContentCompletion) {
let subscriptions = MPTUser.sharedUser.getSubscriptionsForDownload()
DispatchQueue.global().async {
if subscriptions.count > 0 {
var index:Int = 0
var t = subscriptions.count
var downloading: Bool = false
while t != 0 {
if downloading == false {
downloading = true
if index < 0 {
index = 0
}
if index > subscriptions.count - 1 {
index = subscriptions.count - 1
}
if index <= subscriptions.count {
let subscription = subscriptions[index]
if subscription.didDownloadContent == false {
if let subscriptionID = subscription.subscriptionID {
DispatchQueue.main.async {
SVProgressHUD.show(withStatus: "Downloading Documents\nfor\n\(subscription.functionalGroupName!)\n\(index+1) of \(subscriptions.count)")
}
self.getUserSubscribedContent(subscriptionID: subscriptionID, completion: { (success) in
subscription.didDownloadContent = true
index += 1
t -= 1
downloading = false
})
}
else {
index += 1
t -= 1
downloading = false
}
}
}
else {
index += 1
t -= 1
downloading = false
}
}
}
}
completion()
}
}

self.getUserSubscribedContent 是一个下载内容并在 block 中发送完成的函数。

如果有人可以帮助我,我将不胜感激。

最佳答案

您可以尝试使用 DispatchGroup。这是一个粗略的(未经测试的)示例:

DispatchQueue.global().async {
let subscriptions = MPTUser.sharedUser.getSubscriptionsForDownload()
let group = DispatchGroup()
var completed = 0
let completion: (Bool) -> Void = {
if $0 {
completed += 1
}
group.leave()
DispatchQueue.main.async {
SVProgressHUD.show(withStatus: "Downloading Documents\nfor\n\(subscription.functionalGroupName!)\n\(completed) of \(subscriptions.count)")
}
}
for subscription in subscriptions {
self.getUserSubscribedContent(subscriptionID: subscription.subscriptionID, completion: completion)
group.enter()
}
// However long you want to wait (in seconds) before timing out
_ = group.wait(timeout: .now() + 30)
}

关于ios - GCD : Making Sequential Download Calls; ONE BY ONE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41643318/

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