1

我在里面调用了一个函数DispatchQueue.main.async。这是我的代码:

let group = DispatchGroup()
group.enter()

DispatchQueue.main.async {
        for i in 0 ... (Global.selectedIcons.count - 1) {
            if self.albumorphoto == 1 {
                if i == 0 {
                    self.detector = 1
                    self.uploadPhoto() //here
                }
                else {
                    self.detector = 2
                    self.uploadPhoto() //here
                }
            }
            else {
                self.uploadPhoto() //here
            }
        }
        group.leave()
    }

    group.notify(queue: .main) {
        print("done")
    }
}

func uploadPhoto(){
    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

    let params = param

    request.httpBody = params.data(using: String.Encoding.utf8)

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print("error=\(error!)")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response!)")
        }

        let responseString = String(data: data, encoding: .utf8)
        print("responseString = \(responseString!)")

        if self.detector == 1 {
            self.album = self.parseJsonData(data: data)
        }

    }

    task.resume()
}

func parseJsonData(data: Data) -> [AnyObject] {
    do {
        let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
        let jsonalbum = jsonResult!["data"] as? [AnyObject]
        for jsonAlbum in jsonalbum! {
            self.folderID = jsonAlbum["id"] as! String
        }
    } catch {
        print(error)
    }

    return album
}

我希望让它等到所有任务DispathcQueue完成。它有效,但问题是我的功能uploadPhoto()。它不能等到uploadPhoto()完成它的任务。有什么办法解决这个问题吗?谢谢!

4

1 回答 1

2

在这里使用 aDispatchGroup是正确的选择,但是对于每个异步任务,您都必须这样enterleave

let group = DispatchGroup()
photos.forEach { photo in

    group.enter()
    // create the request for the photo

    URLSession.shared.dataTask(with: request) { data, response, error in

        group.leave()
        // handle the response

    }.resume()
}
group.notify(queue: .main) {
    print("All photos uploaded.")
}

您不需要DispatchQueue.async()调用,因为URLSession.shared.dataTask已经是异步的。

在我的代码中,我假设您要将对象建模为数组Photo并替换Global.selectedIcons.count为:photos

class Photo {
    let isAlbum: Bool
    let isDefector: Bool
    let imageData: Data
}

我建议您看看AlamofireSwiftyJSON以进一步改进您的代码。这些是流行的库,可以更轻松地处理网络请求。有了它们,您几乎可以将整个uploadPhoto()/parseJsonData()功能简化为以下内容:

Alamofire.upload(photo.imageData, to: url).responseSwiftyJSON { json in
    json["data"].array?.compactMap{ $0["id"].string }.forEach {
        self.folderID = $0
    }
}

这使您的代码更加稳定,因为它消除了所有强制展开。Alamofire 还为您提供上传进度和恢复和取消请求等功能。

于 2018-04-10T22:49:54.487 回答