gpt4 book ai didi

swift - 在请求完成之前执行的 Completion Handler

转载 作者:行者123 更新时间:2023-11-28 13:34:06 24 4
gpt4 key购买 nike

我有一个使用 UIImagePickerController 来选择图像的应用程序。然后将该图像传递到 API 函数中。该功能完成后,我使用委托(delegate)将结果传递给带有结果的模态显示 Controller 。但是,模态 Controller 出现在完成 block 之前,我的错误 AlerViewController 警报从未被调用。

API 在后台线程中运行,我在主线程上设置了完成(因为它更新了 UI - 呈现模态 Controller )但是在完成完全执行之前它被调用了。

下面的代码;

API请求

func searchImage(with image: UIImage, to viewController: UIViewController, success: @escaping([ViImageResult]?) -> Void) {
var results = [ViImageResult]()
let params = ViUploadSearchParams(image: image)
ViSearch.sharedInstance.uploadSearch(params: params, successHandler: { (data : ViResponseData?) -> Void in
guard let data = data else { return }
if data.result.isEmpty {
AlertViewController.noResultsFound(viewController: viewController)
return
} else {
if data.hasError {
AlertViewController.dataError(viewController: viewController)
return
} else {
for response in data.result {
results.append(response)
}
DispatchQueue.main.async {
success(results)
}
}
}
}, failureHandler: {
(error) -> Void in
AlertViewController.dataError(viewController: viewController)
})
}

Controller

var selectedImage: UIImage? {
didSet {
guard let selectedImage = selectedImage else { return }
ViSearchSDKService.shared.searchImage(with: selectedImage, to: self) { (results) in
guard let results = results else { return }
if self.resultsDelegate != nil {
self.resultsDelegate?.recievedResults(recievedResults: results)
}
}
let resultsController = ResultsViewController()
self.resultsDelegate = resultsController
let navigationController = UINavigationController(rootViewController: resultsController)
navigationController.modalPresentationStyle = .overFullScreen
DispatchQueue.main.async {
self.present(navigationController, animated: true, completion: nil)
}
}
}

在 API 请求中,我所有的 AlertViewController 函数都在主线程上调用,然后从请求中返回。成功 block 也在主线程上调用。

我在这里做错了什么?...

更新

我不太确定为什么会这样,但它可以满足我的所有需求。我已将 API 请求移动到

之外的另一个函数中
var selectedImage: UIImage? {
didSet {

在我的 Controller 中。

新工作代码

var selectedImage: UIImage? {
didSet {
guard let selectedImage = selectedImage else { return }
self.searchImage(with: selectedImage)
}
}

func searchImage(with image: UIImage) {
ViSearchSDKService.shared.searchImage(with: image, to: self) { (results) in
guard let results = results else { return }
let resultsController = ResultsViewController()
self.resultsDelegate = resultsController
if self.resultsDelegate != nil {
self.resultsDelegate?.recievedResults(recievedResults: results)
}
let navigationController = UINavigationController(rootViewController: resultsController)
navigationController.modalPresentationStyle = .fullScreen
DispatchQueue.main.async {
self.present(navigationController, animated: true, completion: nil)
}
}
}

最佳答案

我想你想要这个。

    var selectedImage: UIImage? {
didSet {
// make sure it was not set to nil
guard let selectedImage = selectedImage else { return }

// set up your view controller for the response
let resultsController = ResultsViewController()
self.resultsDelegate = resultsController
let navigationController = UINavigationController(rootViewController: resultsController)

// do your search
ViSearchSDKService.shared.searchImage(with: selectedImage, to: self) { (results) in
// leave no path without visible side-effect
guard let results = results else { debugPrint("nil results"); return }

// now that we have the result, present your results view controller
navigationController.modalPresentationStyle = .overFullScreen
DispatchQueue.main.async {
self.present(navigationController, animated: true) { in

// once done presenting, let it know about the results
self.resultsDelegate?.recievedResults(recievedResults: results)
}
}
}
}
}

关于swift - 在请求完成之前执行的 Completion Handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56913678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com