tnev
tnev

Reputation: 386

Performing a Segue Inside of a Completion Handler

I'm attempting to perform a storyboard segue inside of a completion handler like so:

movieWriter.finishRecordingWithCompletionHandler({ () -> Void in
            //Leave this view
            self.performSegueWithIdentifier("decisionSegue", sender: self)
        })

and getting the following warning:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.

The completion handler is running on a background so I understand why I'm getting this error, my question is what are my options for performing this segue without getting this error?

The reason I'm performing the segue in the completion handler is that the completion handler is called after a recorded movie is done being written to file and the view being segued to plays the movie, hence it needs to be on file before segueing.

Upvotes: 0

Views: 2053

Answers (3)

Muhammad Waqas Bhati
Muhammad Waqas Bhati

Reputation: 2805

This Error is telling that You are performing some UI update task from Background Thread and it's not possible to Update UI from background thread so you have to access main Thread and then perform segue.

Objective-C:

dispatch_async(dispatch_get_main_queue(), ^{
    // update some UI
    // Perform your Segue here
});

Swift:

DispatchQueue.main.async {
    // update some UI
    // Perform your Segue here    
}

Hope it will help you.

Upvotes: 2

nikhil84
nikhil84

Reputation: 3207

Whenever your perform any operation on UI/active view then it has to be on main thread and not background thread.

Do as following:

__weak typeof(self) weakSelf = self; //Best practice
                                     //Provide a weak reference in block and not strong.

movieWriter.finishRecordingWithCompletionHandler({ () -> Void in

     dispatch_async(dispatch_get_main_queue(),{

        weakSelf.performSegueWithIdentifier("decisionSegue", sender:weakSelf)

     })            
})

Upvotes: 4

Ashish Kakkad
Ashish Kakkad

Reputation: 23872

Put it in dispatch queue :

dispatch_async(dispatch_get_main_queue(),{
    self.performSegueWithIdentifier("decisionSegue", sender: self)
})

Hope it will work

For more detailed information : This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes

Upvotes: 4

Related Questions