swift - 如何在swift中的for循环中使用异步任务

标签 swift uitableview for-loop asynchronous nsurlsession

我在我的项目中使用谷歌地图。我正在计算当前位置的位置距离,并显示这是 TableView。现在有多个地方,我想在 tableView 中显示它。对于每个地方及其与当前位置的距离,我需要调用 google distanceMatrix Api。我能够将 Places 坐标存储在数组中并循环遍历我调用 distance martix api 的数组。

这是代码

 func calculateDistance(type : String)
    {
        let currentLocationCordinate = "\(userCurrentLocation.coordinate.latitude),\(userCurrentLocation.coordinate.longitude)"

        var url = String()
        var remoteUrl = NSURL()
        var request = NSURLRequest()
        var session = NSURLSession()
        var locatioArrayIndex = 0

        //locationArray Stores Cordinate of nearby array
        for locatioArrayIndex in 0...locationArray.count-1

        {
            placeCordinationArray = "\(locationArray[locatioArrayIndex].coordinate.latitude),\(locationArray[locatioArrayIndex].coordinate.longitude)"

            url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(currentLocationCordinate)&destinations=\(placeCordinationArray)&key=\(key)"


            remoteUrl = NSURL(string: url)!

            request = NSURLRequest(URL: remoteUrl)
            let config1 = NSURLSessionConfiguration.defaultSessionConfiguration()
            session = NSURLSession(configuration: config1)

            // Asych start here
            let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in


            // Convert server json response to NSDictionary
            do {

                let convertedJsonIntoDict = try NSJSONSerialization.JSONObjectWithData(data!, options: [])

                let rows = convertedJsonIntoDict["rows"] as! NSArray

                let elements = rows[0]

                let element1 = elements["elements"] as! NSArray

                let distance = element1[0]

                let distanceDictionary = distance["distance"]

                let testDistance = distanceDictionary!!["text"]  as! String!
                self.distanceMeter = testDistance
                self.placeDistantArray.append(self.distanceMeter)

                    if (locatioArrayIndex == 9)
                    {
                    dispatch_async(dispatch_get_main_queue(),
                        {
                            self.segueCheck = true
                            self.searchActivityIndicator.stopAnimating()
                            self.searchActivityIndicator.hidesWhenStopped = true
                            self.tableView.reloadData()
                   })
                }
                    //
                    print("LOCATION ARRAY INDEXINSIDE",locatioArrayIndex)

                }

                catch let error as NSError
                {
                    print("SOME ERROR ",error.localizedDescription)
                }
            })

            task.resume()


        }// For Loop CLosed

      }

我将距离存储在 self.placeDistantArray 数组中。然后用它的数据重新加载 tableview。但 self.placeDistantArray 中的值有时会重复。和 tableview 有时会出现数组索引错误

最佳答案

你的代码非常非常不干净而且根本不可读,所以我清理了它:

func calculateDistanceUpdatingUI(type : String)
{
    //locationArray Stores Cordinate of nearby array
    for (index, location) in locationArray.enumerate()
    {
        if let remoteUrl = distanceCalculationURLFrom(userCurrentLocation, toDestination: location) {
            let request = NSURLRequest(URL: remoteUrl)
            let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

            // clear array
            self.placeDistantArray.removeAll()

            // request distance
            let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

                do
                {
                    if let distanceMeter = try self.testDistanceFrom(data) {
                        self.distanceMeter = distanceMeter
                        self.placeDistantArray.append(distanceMeter)
                    }

                    if (index == 9)
                    {
                        self.updateUI()
                    }

                    print("LOCATION ARRAY INDEXINSIDE", index)
                }
                catch let error as NSError
                {
                    print("SOME ERROR ", error.localizedDescription)
                }
            })

            task.resume()
        }
    }
}

private func updateUI()
{
    dispatch_async(dispatch_get_main_queue())
    {
        self.segueCheck = true
        self.searchActivityIndicator.stopAnimating()
        self.searchActivityIndicator.hidesWhenStopped = true
        self.tableView.reloadData()
    }
}

private func distanceCalculationURLFrom(origin: CLLocation, toDestination destination: CLLocation) -> NSURL?
{
    var urlString = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="
    urlString += "\(origin.coordinate.latitude),\(origin.coordinate.longitude)&destinations="
    urlString += "\(destination.coordinate.latitude),\(destination.coordinate.longitude)&key=\(key)"
    return NSURL(string: urlString)
}

private func testDistanceFrom(data: NSData?) throws -> String?
{
    guard let data = data else {
        return nil
    }
    let convertedJsonIntoDict = try NSJSONSerialization.JSONObjectWithData(data, options: [])
    if let rows = convertedJsonIntoDict["rows"] as? NSArray where rows.count > 0,
        let element1 = rows[0]["elements"] as? NSArray where element1.count > 0,
        let distanceDictionary = element1[0]["distance"] as? NSDictionary,
        let testDistance = distanceDictionary["text"] as? String
    {
        return testDistance
    }

    return nil
}

这里有一些建议:

  • 您不应该轻易使用强制解包。
  • 你应该尽量避免使用旧式的 for 循环,尽量使用快速枚举(更具可读性)。
  • 你的函数不应该有副作用。如果你调用你的函数calculateDistance,它应该这样做,计算距离。不更改 placeCordinationArray 等变量或更新 UI。
  • 您应该尝试拆分代码并制作更小的函数。如果您必须添加像 //For Loop CLosed 这样的注释,那肯定太大了
  • 你不应该初始化你不会使用的对象(如 var request = NSURLRequest())
  • 尝试使用正确的词,例如 coordinate 而不是 cordinationlocation 而不是 locatio

正如@Jigar Tarsariya 所说,我还在阵列再次填充之前清理了它。

关于swift - 如何在swift中的for循环中使用异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429062/

相关文章:

c - 如何在不使用 for 循环的情况下填充二维数组?

c - 使用递归打印控制台 "picture"

R查找平均订单间隔(天数)

swift - 以编程方式在indexpath.item处呈现一个UIViewController(嵌入在UITabBarController中)

swift - TouchBegan 函数的计时器

ios - 预加载 UITableView 单元格

ios - 如何从一个类调用另一个类的委托(delegate)方法

iOS JSON 从 web 解析到 UITableView

ios - 在 Swift 3 iOS 8 应用程序中集成 GIF

ios - UIScrollView 不随 UICollectionView 一起滚动