如何阻止任何事情发生直到func完成?

时间:2018-06-01 15:22:20

标签: ios swift function

我一直在四处寻找,并且在功能完成之前没有找到防止任何事情发生的方法。基本上,函数之下的事件发生在函数之前。让我告诉你代码和控制台输出。

代码:

if locationSwitch.isOn == true {
            let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(location.latitude),\(location.longitude)&result_type=locality&key=AIzaSyDI-ZacHyPbLchRhkoaUTDokwj--z_a_jk"
            loadUrl(url: url) { optionalLocation in
                guard let nonOptionalLocation = optionalLocation else {
                    // Location was nil; Handle error case here
                    return
                }
                // Do something with your location here, like setting UI or something
                print("Optional Location: \(optionalLocation)")
                print("nonOptional Location: \(nonOptionalLocation)")
                cityState = nonOptionalLocation
                print("cityState: \(cityState)")
                print("Within Func")
            }
            print("Within If")
        } else {
            lat = 0.0
            lng = 0.0
            cityState = ""
        }
        print("After If")
        print("cityState Outside If Statement: \(cityState)")
        CoreDataHandler.savePhotoObject(locationCoordinateLatitude: lat, locationCoordinateLongitude: lng, locationLocality: cityState, dateCreated: date, discription: discriptionBox.text!, photo: selectedImageData!)
    }

控制台输出:

Before If
Within If
After If
cityState Outside If Statement:
Optional Location: Optional("San Francisco, CA, USA")
nonOptional Location: San Francisco, CA, USA
cityState: San Francisco, CA, USA
Within Func

如你所见

Optional Location: Optional("San Francisco, CA, USA")
nonOptional Location: San Francisco, CA, USA
cityState: San Francisco, CA, USA
Within Func

应该在

之前
Within If

我相信这种情况正在发生,因为loadUrl函数需要很长时间才能完成代码覆盖它并完成其下的代码然后loadUrl完成,然后在其他所有内容后打印。除了在nonOptionalLocation函数完成后调用设置为等于loadUrl的变量并且因为loadUrl在其余代码之后结束时,这不会成为问题应该等于nonOptionalLocation的变量实际上等于""

有人知道现在如何完成这项工作我认为我需要阻止任何事情发生,直到loadUrl完成,但如果您知道另一种方法,请分享。谢谢。

2 个答案:

答案 0 :(得分:4)

loadUrl异步工作,只需将数据返回完成块后执行代码

if locationSwitch.isOn == true {
    loadUrl(url: url) { optionalLocation in
        guard let nonOptionalLocation = optionalLocation else {
            // Location was nil; Handle error case here
            return
        }
        // Do something with your location here, like setting UI or something
        print("Optional Location: \(optionalLocation)")
        print("nonOptional Location: \(nonOptionalLocation)")
        cityState = nonOptionalLocation
        print("cityState: \(cityState)")
        print("Within Func")
        DispatchQueue.main.async {
            CoreDataHandler.savePhotoObject(locationCoordinateLatitude: lat, locationCoordinateLongitude: lng, locationLocality: cityState, dateCreated: date, discription: discriptionBox.text!, photo: selectedImageData!)
        }
    }
} else {
    CoreDataHandler.savePhotoObject(locationCoordinateLatitude: 0.0, locationCoordinateLongitude: 0.0, locationLocality: "", dateCreated: date, discription: discriptionBox.text!, photo: selectedImageData!)
}

答案 1 :(得分:0)

您是否使用过DispatchQueue.main.sync {},它可能会暂时停留在您的用户界面中一段时间​​不好但如果您对此没有任何问题,那么您可以试试这个。

if locationSwitch.isOn{

        DispatchQueue.main.sync {

            let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(location.latitude),(location.longitude)&result_type=locality&key=AIzaSyDI-ZacHyPbLchRhkoaUTDokwj--z_a_jk"

            loadUrl(url: url) { optionalLocation in
                guard let nonOptionalLocation = optionalLocation else {
                    return
                }

                print("Optional Location: \(optionalLocation)")
                print("nonOptional Location: \(nonOptionalLocation)")
                cityState = nonOptionalLocation
                print("cityState: \(cityState)")
                print("Within Func")
            }
        }
        print("Within If")
    } else {
        lat = 0.0
        lng = 0.0
        cityState = ""
    }
    print("After If")
    print("cityState Outside If Statement: \(cityState)")
    CoreDataHandler.savePhotoObject(locationCoordinateLatitude: lat, locationCoordinateLongitude: lng, locationLocality: cityState, dateCreated: date, discription: discriptionBox.text!, photo: selectedImageData!)