簡體   English   中英

在功能完成之前,如何阻止任何事情發生?

[英]How to block anything from happening until a func has been completed?

我一直在環顧四周,在功能完成之前沒有找到防止任何事情發生的方法。 基本上,功能之下的事件在功能之前發生。 讓我向您展示代碼和控制台輸出。

碼:

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完成,然后在所有其他內容之后打印出來。 除了在loadUrl函數完成后調用設置為nonOptionalLocation的變量之外,這不會有問題,並且由於loadUrl在其余的代碼之后結束,因此應該等於nonOptionalLocation的變量實際上等於""

有人知道現在如何完成此工作嗎?我認為我需要阻止任何事情發生,直到loadUrl完成為止,但是如果您知道執行此操作的另一種方法,請分享。 謝謝。

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!)
}

您是否使用過DispatchQueue.main.sync {},它可能會在UI中停留一段時間,但這並不好,但是如果對此沒有任何問題,可以嘗試一下。

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!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM