GCD:进行顺序下载调用;逐个
GCD: Making Sequential Download Calls; ONE BY ONE
场景
应用程序逐个下载用户订阅。这个调用将在多个地方进行(在另一个网络调用之后的完成块中以及从 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
是一个下载内容并在块中发送完成的函数。
如果有人能帮助我,我将不胜感激。
您可以尝试使用 DispatchGroup
。这是一个粗略的(未经测试的)示例:
DispatchQueue.global().async {
let subscriptions = MPTUser.sharedUser.getSubscriptionsForDownload()
let group = DispatchGroup()
var completed = 0
let completion: (Bool) -> Void = {
if [=10=] {
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)
}
场景
应用程序逐个下载用户订阅。这个调用将在多个地方进行(在另一个网络调用之后的完成块中以及从 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
是一个下载内容并在块中发送完成的函数。
如果有人能帮助我,我将不胜感激。
您可以尝试使用 DispatchGroup
。这是一个粗略的(未经测试的)示例:
DispatchQueue.global().async {
let subscriptions = MPTUser.sharedUser.getSubscriptionsForDownload()
let group = DispatchGroup()
var completed = 0
let completion: (Bool) -> Void = {
if [=10=] {
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)
}