SCENARIO
该应用逐个下载用户订阅。此呼叫将在多个地方进行(在另一个网络呼叫之后的完成模块中以及从UIAlertController
按下按钮)。逻辑是下载所有订阅,一旦订阅下载停止,它将转到下一个,直到所有订阅都被下载,然后我们的SVProgressHUD
解散。当我们从Xcode构建和运行时,代码工作得很好。但是当我们构建IPA并发送给我们的客户时,这种逻辑会产生某种失速,并且SVProgressHUD
警报会继续旋转:
这是一个大问题,因为我们的应用程序专注于从订阅下载内容。
为什么这个逻辑在我归档并从中构建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
是一个下载内容并在块中发回完成功能的函数。
如果有人可以帮助我,我将非常感激。
答案 0 :(得分:1)
您可以尝试使用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)
}