Распечатка заявления не печатается - PullRequest
0 голосов
/ 20 октября 2018

Почему в моем коде ниже оператор print в блоке viewWillAppear после того, как AlamofireImage загрузил изображение, пропускает остальную часть кода в блоке viewWillAppear ...

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)


    Alamofire.request("https://example.com/four.png").responseImage { response in
        debugPrint(response)

        print(response.request as Any)
        print(response.response as Any)
        debugPrint(response.result)

        if let image = response.result.value {
            print("image downloaded: \(image)")

            self.imageServer.append(image)

            print("ImageServer append Successful")
            print("The new number of images = \(self.imageServer.count)")

        }
    }


///////////THESE STATEMENTS ARE BEING SKIPPED/////////////////
    print("The new number of images = \(imageServer.count)")
    print("Test")
    trackedImages = loadedImagesFromDirectoryContents(imageServer)
    configuration.trackingImages = trackedImages
    configuration.maximumNumberOfTrackedImages = 1
    sceneView.session.run(configuration)
}

enter image description here

1 Ответ

0 голосов
/ 20 октября 2018

Вы можете использовать completionHandler, чтобы решить эту проблему.Который будет выполнен после завершения function.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    fetchImage {
        print("The new number of images = \(imageServer.count)")
        trackedImages = loadedImagesFromDirectoryContents(imageServer)
        configuration.trackingImages = trackedImages
        configuration.maximumNumberOfTrackedImages = 1
        sceneView.session.run(configuration)
    }

}

func fetchImage(completion: @escaping ()->()) {
    Alamofire.request("https://example.com/four.png").responseImage { response in
        debugPrint(response)

        print(response.request as Any)
        print(response.response as Any)
        debugPrint(response.result)

        if let image = response.result.value {
            print("image downloaded: \(image)")

            self.imageServer.append(image)

            print("ImageServer append Successful")
            print("The new number of images = \(self.imageServer.count)")

        }
        completion()
    }
}
...