Swift iOS - 从管理循环中取消DispatchGroup()

时间:2018-05-23 02:07:30

标签: ios swift loops grand-central-dispatch firebase-storage

我循环浏览多个Url,将它们转换为Data,然后将数据发送到Firebase Storage,然后当一切都完成后,将收集的信息发送到Firebase Database < / p>

我使用DispatchGroup()的.enter()来启动循环,一旦我将数据发送到存储并获取值url string absoluteString,我使用.leave()开始下一次迭代

我意识到在循环过程中,有几个点可能发生错误:

  1. 进入UrlSession
  2. 一旦进入Storage的.putData功能
  3. 一旦进入Storage的.downloadURL(completion:...完成处理程序
  4. 如果最终的downloadURL ?.absoluteString为nil
  5. ,则再次

    如果我在任何一个点上显示错误,我会显示警报功能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(){
    }
    

1 个答案:

答案 0 :(得分:0)

在下面的评论中,问题@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)
}