- Xcode 13.3.1
- Swift 5.6
- iOS 15
Hello.
I found a mysterious spec about RefreshController and AlertController.
see the code.
collectionView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(refreshAction(_:)), for: .valueChanged)
}
@objc func refreshAction(_ selector: UIRefreshControl) {
Task { @MainActor [weak self] in
guard let self = self else { return }
do {
let response = try await api.getData()
//
self.refreshControl.endRefreshing()
} catch {
self.refreshControl.endRefreshing() // could't end refresh indicator.
let alert = UIAlertController(title: "Error", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alert, animated: true, completion: nil)
}
}
}
Why I could't handle endRefreshing when I called AlertController?
I have solution this problem for another way. I would like to solve this problem without using the alert closure if possible.
@objc func refreshAction(_ selector: UIRefreshControl) {
Task { @MainActor [weak self] in
guard let self = self else { return }
do {
let response = try await api.getData()
//
self.refreshControl.endRefreshing()
} catch {
let alert = UIAlertController(title: "Error", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
self.refreshControl.endRefreshing()
}))
self.present(alert, animated: true, completion: nil)
}
}
}