So I'm beginning to learn the basics of Grand Central Dispatch and the whole concept of multithreading with iOS applications. Every tutorial will tell you that you must run UI events on the main thread, but I don't completely understand why.
Here's a problem I came across yesterday, and finally fixed it by running a segue on the main thread, but I still don't understand why running it off the main thread was a problem:
I had a custom initial VC (barcode scanner) and a segue to a new view controller with a UIWebView
attached. As soon as the VC found a barcode, it called a handler, and in that closure, I had a performSegueWithIdentifier
. However, I got a EXC_BAD_ACCESS
because of this (it didn't happen when the second VC had a label or a UIImageView
, just with UIWebView
). I finally realized that for some reason, the closure was called off the main thread, and thus the segue was being performed off the main thread. Why exactly would performing the segue on another thread throw a memory error? Is it because self
in self.performSegueWithIdentifier
was somehow nil? And why wouldn't Swift automatically dispatch a segue event on the main thread?