Thanks, eskimo!
I have tied the timer which works.
But since in the application this problem only appears in a kind of connection test process I decided to start the connection and show an ALERT with CANCEL button. The ALERT will be closed on reaching .cancelled state. Connection is cancelled on state .ready, .waiting and .failure as well as when CANCEL is pressed. The I inform the main thread about the result by a Notification:
if alert == nil {
alert = UIAlertController(title: "Test connection", message: "Please wait...", preferredStyle: .alert)
if let alert = alert {
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
self.testConnection.forceCancel()
NotificationCenter.default.post(name: Notification.Name("connectionTest"), object: "Canceled by user!")
self.alert = nil
return
}))
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.medium
loadingIndicator.startAnimating()
alert.view.addSubview(loadingIndicator)
present(alert, animated: false, completion: nil)
}
}
}