dispatch_group_notify没有被调用

时间:2015-12-01 21:30:20

标签: ios swift grand-central-dispatch

我正在尝试将几个位置转换为[CLPlacemark]以在地图上绘制路径。但是dispatch_group_notify并没有调用我的完成块。

ViewController.swift

var locationManager:LocationManager!

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager = LocationManager(company: companyType, locations: locations)
    locationManager.getPointsfromLocations { points in
        print("Original Locations",locations)
        print("Converted Points",points)
    }
}

LocationManager.swift

protocol Locatable: class {
     static func normalizeLocation(location:String) ->String
 }

class LocationManager {
    var company:BaseCompany.Type
    var locations:[String]

init(company:BaseCompany.Type,locations:[String]) {
    self.company = company
    self.locations = locations
}

func getPointsfromLocations(completion: (points: [CLPlacemark]) -> ()){
    let geocoder = CLGeocoder()
    var points = [CLPlacemark]()
    let group = dispatch_group_create()

        for location in self.locations {
            dispatch_group_enter(group)
            var normalizedLocation = ""
            if let companyObject = self.company as? Locatable.Type {
                normalizedLocation = companyObject.normalizeLocation(location)
            } else {
                normalizedLocation = location
            }
            geocoder.geocodeAddressString(normalizedLocation)
                { placemarks, error in
                    if let placemark = placemarks?[0] {
                        points.append(placemark)
                    }
                    dispatch_group_leave(group)
            }
        }

    dispatch_group_notify(group,dispatch_get_main_queue()) {
            completion(points: points)

        }
  }
}

0 个答案:

没有答案