iOS Swift - 数据获取速度不够快,无法到达另一个 UIViewController
iOS Swift - Data is not fetched quickly enough to reach another UIViewController
我有一个源视图控制器和一个目标视图控制器。
我想在移动到此屏幕("show" segue)后显示从 UI(目标)中的 url 获取的数据。
为了做到这一点,在源代码 VC 中我使用了 "prepare for segue" 方法,我在其中调用了一个函数 returns 一个包含我想要显示的所有获取数据的数组并将其传递到目标 VC 以显示在 UITableView 中。
问题是很多时候整个数据都没有从 url 中获取,所以我将一个空数组传递给目标。
这是源码中的代码VC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVc = segue.destination as?
ShowCharacterInfoViewController{
fetchCharacterArrayFromUrl(characterName: cellContent){ results in
destinationVc.array.append(contentsOf:results)
destinationVc.tabelView.reloadData()}
} }
我想不出合适的解决方案。
您需要更新主线程中的 UI 内容,因为您正在执行后端调用:-
DispatchQueue.main.async({
destinationVc.tabelView.reloadData()
})
你可以做以下两件事之一。
- 加载数据并仅在函数返回数据后执行 segue。
转换到目标屏幕并让控制器加载 viewWillAppear 中的数据。
// Add a place to save the results
var saveResults: MyResultsType?
@IBAction func someButtonAction(_ sender: Any) {
// Consider putting a "Loading..." dialog here
fetchCharacterArrayFromUrl(characterName: cellContent){ results in
self.saveResults = results
DispatchQueue.main.async({
// Hide the "Loading..." dialog
performSegue(withIdentifier: "ID of your Segue", sender: sender) //
})
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVc = segue.destination as? ShowCharacterInfoViewController {
// I'm not sure append() is really what you need here
destinationVc.array.append(contentsOf: savedResults)
destinationVc.tabelView.reloadData()
}
}
我有一个源视图控制器和一个目标视图控制器。 我想在移动到此屏幕("show" segue)后显示从 UI(目标)中的 url 获取的数据。
为了做到这一点,在源代码 VC 中我使用了 "prepare for segue" 方法,我在其中调用了一个函数 returns 一个包含我想要显示的所有获取数据的数组并将其传递到目标 VC 以显示在 UITableView 中。 问题是很多时候整个数据都没有从 url 中获取,所以我将一个空数组传递给目标。
这是源码中的代码VC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVc = segue.destination as?
ShowCharacterInfoViewController{
fetchCharacterArrayFromUrl(characterName: cellContent){ results in
destinationVc.array.append(contentsOf:results)
destinationVc.tabelView.reloadData()}
} }
我想不出合适的解决方案。
您需要更新主线程中的 UI 内容,因为您正在执行后端调用:-
DispatchQueue.main.async({
destinationVc.tabelView.reloadData()
})
你可以做以下两件事之一。
- 加载数据并仅在函数返回数据后执行 segue。
转换到目标屏幕并让控制器加载 viewWillAppear 中的数据。
// Add a place to save the results var saveResults: MyResultsType? @IBAction func someButtonAction(_ sender: Any) { // Consider putting a "Loading..." dialog here fetchCharacterArrayFromUrl(characterName: cellContent){ results in self.saveResults = results DispatchQueue.main.async({ // Hide the "Loading..." dialog performSegue(withIdentifier: "ID of your Segue", sender: sender) // }) } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let destinationVc = segue.destination as? ShowCharacterInfoViewController { // I'm not sure append() is really what you need here destinationVc.array.append(contentsOf: savedResults) destinationVc.tabelView.reloadData() } }