对于循环swift xcode,没有调用DispatchGroup.notify

问题描述 投票:0回答:2

我有一个for循环,通过searchRequest到google的结果递增。我使用DispatchGroup来确保在更新UI之前从searchRequest获取所有数据。但是,我的dispatchGroup.notify函数没有被调用,因此我的UI永远不会被更新。见下面的代码:

func donationCenters(completion: @escaping ([DonationCenter])->()) {

    var placeSearchQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(String(self.coordinates.latitude)),\(String(self.coordinates.longitude))&radius=1500&keyword=donation center&key=###########"

    // GROUP CREATED
    let myGroup = DispatchGroup()

    placeSearchQuery = placeSearchQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

    var urlRequest = URLRequest(url: URL(string: placeSearchQuery)!)
    urlRequest.httpMethod = "GET"

    let task = URLSession.shared.dataTask(with: urlRequest) { (data, resopnse, error) in
        if error == nil {
            let jsonDict = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
            if let dict = jsonDict as? Dictionary<String, AnyObject> {
                if let results = dict["results"] as? [Dictionary<String, AnyObject>] {
                    var donationCenters: [DonationCenter] = []


                    for result in results {
                        myGroup.enter() // ENTER GROUP
                            let donationCenter = DonationCenter(name: text, image: image, latitude: location["lat"] as! Double, longitude: location["lng"] as! Double, phone: formattedPhoneNumber, website: website)
                            donationCenters.append(donationCenter)
                            myGroup.leave() // LEAVE GROUP
                    }


                    // NOTIFY - NEVER CALLED (done never printed)
                    myGroup.notify(queue: .main) {
                        print("done")
                        completion(donationCenters)
                    }
                }
            }
        } else {
            // Error with search request
        }
    }
    task.resume()
}

更新的代码:

func donationCenters(completion: @escaping ([DonationCenter])->()) {
    if let coordinates = self.coordinates {
        var placeSearchQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(String(coordinates.latitude)),\(String(coordinates.longitude))&radius=1500&keyword=donation center&key=###########"
        placeSearchQuery = placeSearchQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        var urlRequest = URLRequest(url: URL(string: placeSearchQuery)!)
        urlRequest.httpMethod = "GET"
        let task = URLSession.shared.dataTask(with: urlRequest) { (data, resopnse, error) in
            if error == nil {
                let jsonDict = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
                if let dict = jsonDict as? Dictionary<String, AnyObject> {
                    if let results = dict["results"] as? [Dictionary<String, AnyObject>] {
                        var donationCenters: [DonationCenter] = []
                        for result in results {
                            let text = result["name"] as! String
                            if let images = result["photos"] as? [Dictionary<String, AnyObject>] {
                                if let photoReference = images[0]["photo_reference"] as? String {
                                    self.imageForPhotoReference(photoReference, completion: { image in
                                        if let image = image {
                                            if let placeId = result["place_id"] as? String {
                                                var placeDetailsQuery = "https://maps.googleapis.com/maps/api/place/details/json?placeid=\(placeId)&fields=name,formatted_phone_number,website,formatted_address,geometry&key=########"
                                                placeDetailsQuery = placeDetailsQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
                                                var urlRequest = URLRequest(url: URL(string: placeDetailsQuery)!)
                                                urlRequest.httpMethod = "GET"
                                                let task = URLSession.shared.dataTask(with: urlRequest) { (data, resopnse, error) in
                                                    if error == nil {
                                                        let jsonDict = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
                                                        if let dict = jsonDict as? Dictionary<String, AnyObject> {
                                                            if let result = dict["result"] as? Dictionary<String, AnyObject> {
                                                                if let formattedPhoneNumber = result["formatted_phone_number"] as? String {
                                                                    if let website = result["website"] as? String {
                                                                        if let geometry = result["geometry"] as? Dictionary<String, AnyObject> {
                                                                            if let location = geometry["location"] as? Dictionary<String, AnyObject> {




                                                                                let donationCenter = DonationCenter(name: text, image: image, latitude: location["lat"] as! Double, longitude: location["lng"] as! Double, phone: formattedPhoneNumber, website: website)
                                                                                donationCenters.append(donationCenter)



                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    } else {
                                                        //we have error connection google api
                                                    }
                                                }
                                                task.resume()
                                            }
                                        }
                                    })
                                    // IF EMPTY RETURN NO REGISTERED DONATION CENTERS IN YOUR AREA
                                }
                            }
                        }
                        print("done")
                        completion(donationCenters)
                    }
                }
            } else {
                // Error with search request
            }
        }
        task.resume()
    }
}
swift xcode grand-central-dispatch dispatch
2个回答
0
投票

为了能够理解它是如何工作以及如何使用它,首先尝试简化代码并在以后使用所有功能扩展它。

让我们看看你的代码的这一部分

for result in results {
    myGroup.enter() // ENTER GROUP
    let donationCenter = DonationCenter(name: text, image: image, latitude: location["lat"] as! Double, longitude: location["lng"] as! Double, phone: formattedPhoneNumber, website: website)
    donationCenters.append(donationCenter)
    myGroup.leave() // LEAVE GROUP
 }

因为那里只有同步代码,所以相当于

for result in results {
    myGroup.enter()
    myGroup.leave()
}

最后到

for result in results {}

如你所见,你的小组在那里没有任何功能!

打开您的Playground并尝试了解如何根据您的要求使用DispatchGroup(在所有后台任务完成时通知)

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

// it mimics some function which do some job in the background
// as dataTask in your example
func asyncFoo(id: Int, completition: @escaping (_ result: String)->()) {
    let q = DispatchQueue(label: "internal", qos: .background, attributes: .concurrent)
    q.async {
        // running in the backgroud
        var sum  = 0
        for i in 1...1000 {
            let r = Int.random(in: 0..<i)
            sum += r
        }
        let res = sum.description
        completition(res)
    }
}

let group = DispatchGroup()

for i in 0..<10 {
    group.enter() // enter the group before the task starts
    asyncFoo(id: i) { (result) in
        print("id:", i, result)
        group.leave() // leave the group when task finished
    }
}
group.notify(queue: .main) {
    print("all done")
    PlaygroundPage.current.finishExecution()
}

print("continue execution ...")

它打印出类似的东西

continue execution ...    
id: 4 260320
id: 2 252045
id: 8 249323
id: 3 265640
id: 0 256478
id: 1 253038
id: 5 252521
id: 9 255435
id: 6 245125
id: 7 252262
all done

0
投票

在一些帮助下,我发现for循环是造成问题的原因。相反,我菊花链接了多个searchRequests,如下所示:

if results.count>0 {
            guard let placeId0 = results[0]["place_id"] as? String else { return }
            self.placeDetails(placeId0, completion: { (donationCenter0) in
                if results.count-1>0 {
                    guard let placeId1 = results[1]["place_id"] as? String else { return }
                    self.placeDetails(placeId1, completion: { (donationCenter1) in
                        if results.count-2>0 {
                            guard let placeId2 = results[2]["place_id"] as? String else { return }
                            self.placeDetails(placeId2, completion: { (donationCenter2) in
                                let donationCenters = [donationCenter0, donationCenter1, donationCenter2]
                                complete(donationCenters)
                            })
                        }
                    })
                }
            })
        }

然后在completionHandler中调用函数时,我填充一个数组并更新我的UI。感谢大家的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.