Swift 3 - DispatchQueue内部的功能

时间:2018-04-09 05:35:42

标签: swift dispatch-queue

我在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()完成任务。有什么想法解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:2)

使用DispatchGroup是正确的选择,但每个异步任务都必须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还为您提供上传进度和恢复等功能。取消请求。