fasih.ahmed fasih.ahmed - 1 month ago 9
Swift Question

View shown after programmatically triggered segue doesn't update immediately

I created two views and then connect one to the other by ctrl-dragging and creating a segue. Gave this segue the identifier "functionOutput1"

I then created a function and programmatically triggered the segue created above based on certain output in a function.

performSegue(withIdentifier: "functionOuput1", sender: self)


The view segues okay but the contents of the view don't appear until a minute later.

Is there something else I need to be doing here?

EDIT:
I'm performing this segue in a callback function for a URLRequest

let task = session.dataTask(with: request as URLRequest, completionHandler: {
data, response, error -> Void in

if (error != nil) {
// there is an error with the request
print("error: ", error)

} else {
// Request was successful. Check the contents of the response.
print("response: ", response)

let httpResponse = response as! HTTPURLResponse
if httpResponse.statusCode != 200 {
print("problems with server request")

} else {
print("request successful")
//go to next page
performSegue(withIdentifier: "functionOuput1", sender: self)
}
}
})

Answer

This could be a threading issue. Because the data is returned asynchronously. You need to run the view updates on the main thread. You can do this with Swift 3.0 with the following code:

DispatchQueue.main.async {
    // Your view updates here                
}