[英]Swift iOS -how to cancel DispatchGroup() from managing a loop
我遍历几个Url
,将它们转换为Data
,然后将数据发送到Firebase Storage
,然后在一切完成后将收集到的信息发送到Firebase Database
我使用 DispatchGroup() 的.enter()
开始循环,一旦我将数据发送到 Storage 并获得值 url 字符串absoluteString
我使用.leave()
开始下一次迭代。
我意识到在循环过程中,有几个点可能会发生错误:
UrlSession
.putData
函数.downloadURL(completion:...
完成处理程序?.absoluteString
为零如果我在其中任何一点出现错误,我会显示一个警报函数showAlert(
) 来显示警报,并使用session.invalidateAndCancel()
取消 UrlSession。 我取消了一切,因为我希望用户重新开始。
由于 DispatchGroup() 留在.enter()
,我如何取消 DispatchGroup() 以停止循环?
var urls = [URL]()
var picUUID = UUID().uuidString
var dict = [String:Any]()
let session = URLSession.shared
let myGroup = DispatchGroup()
var count = 0
for url in urls{
myGroup.enter()
session.dataTask(with: url!, completionHandler: {
(data, response, error) in
if error != nil {
self.showAlert() // 1st point of error
return
}
DispatchQueue.main.async{
self.sendDataToStorage("\(self.picUUID)_\(self.count).jpg", picData: data)
self.count += 1
}
}).resume()
myGroup.notify(queue: .global(qos: .background) {
self.sendDataFromDictToFirebaseDatabase()
self.count = 0
self.session.invalidateAndCancel()
}
}
func sendDataToStorage(_ picId: String, picData: Data?){
dict.updateValue(picId, forKey:"picId_\(count)")
let picRef = storageRoot.child("pics")
picRef.putData(picData!, metadata: nil, completion: { (metadata, error) in
if error != nil{
self.showAlert() // 2nd point of error
return
}
picRef?.downloadURL(completion: { (url, error) in
if error != nil{
self.showAlert() // 3rd point of error
return
}
if let picUrl = url?.absoluteString{
self.dict.updateValue(picUrl, forKey:"picUrl_\(count)")
self.myGroup.leave() //only leave the group if a Url string was obtained
}else{
self.showAlert() // 4th point of error
}
})
})
}
func showAlert(){
// the DispatchGroup() should get cancelled here
session.invalidateAndCancel()
count = 0
UIAlertController...
}
func sendDataFromDictToFirebaseDatabase(){
}
在问题下方的评论中,@rmaddy 说“无论成功与否,您都需要leave
”。 我这样做了,但循环仍然运行并且sendDataFromDictToFirebaseDatabase()
仍然触发事件,尽管有错误。
我能找到的唯一解决方法是将循环放在一个带有完成处理程序的函数中,并使用bool
来决定sendDataFromDictToFirebaseDatabase()
是否应该触发:
var urls = [URL]()
var picUUID = UUID().uuidString
var dict = [String:Any]()
let session = URLSession.shared
let myGroup = DispatchGroup()
var count = 0
var wasThereAnError = false // use this bool to find out if there was an error at any of the error points
func loopUrls(_ urls: [URL?], completion: @escaping ()->()){
for url in urls{
myGroup.enter()
session.dataTask(with: url!, completionHandler: {
(data, response, error) in
if error != nil {
self.showAlert() // 1st point of error. If there is an error set wasThereAnError = true
return
}
DispatchQueue.main.async{
self.sendDataToStorage("\(self.picUUID)_\(self.count).jpg", picData: data)
self.count += 1
}
}).resume()
myGroup.notify(queue: .global(qos: .background) {
completion()
}
}
}
// will run in completion handler
func loopWasSuccessful(){
// after the loop finished this only runs if there wasn't an error
if wasThereAnError == false {
sendDataFromDictToFirebaseDatabase()
count = 0
session.invalidateAndCancel()
}
}
func sendDataToStorage(_ picId: String, picData: Data?){
dict.updateValue(picId, forKey:"picId_\(count)")
let picRef = storageRoot.child("pics")
picRef.putData(picData!, metadata: nil, completion: { (metadata, error) in
if error != nil{
self.showAlert() // 2nd point of error. If there is an error set wasThereAnError = true
return
}
picRef?.downloadURL(completion: { (url, error) in
if error != nil{
self.showAlert() // 3rd point of error. If there is an error set wasThereAnError = true
return
}
if let picUrl = url?.absoluteString{
self.dict.updateValue(picUrl, forKey:"picUrl_\(count)")
self.myGroup.leave() // leave group here if all good on this iteration
}else{
self.showAlert() // 4th point of error. If there is an error set wasThereAnError = true
}
})
})
}
func showAlert(){
wasThereAnError = true // since there was an error set this to true
myGroup.leave() // even though there is an error still leave the group
session.invalidateAndCancel()
count = 0
UIAlertController...
}
func sendDataFromDictToFirebaseDatabase(){
}
并使用它:
@IBAction fileprivate func postButtonPressed(_ sender: UIButton) {
wasThereAnError = false // set this back to false because if there was an error it was never reset
loopUrls(urls, completion: loopWasSuccessful)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.