I have a setup as such:
self.webView = WKWebView(frame: frame, configuration: WKWebViewConfiguration())
self.externView = UIView(frame: frame)
self.externWeb
Swift 3:
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
// enter code here
}
}
@MobileMon told you how to fix your problem, but not why.
The UIKit is not thread-safe, meaning that must make all UIKit calls from the main thread. Setting a view's hidden flag is a UIKit call, so it needs to be done from the main thread.
If you manipulate UIView objects from a background thread results are undefined. Most of the time the result is that it takes a long time for the change to take effect (what you are seeing), but sometimes the change doesn't happen at all, sometimes the result is bizarre visual effects, and sometimes your app will crash.
Whenever you manipulate a UIView object from a background thread, you need to wrap it in a call to dispatch_async(dispatch_get_main_queue())
as in MobileMon's answer (Or some other approach that causes the code to be run from the main thread, but this is the preferred way.)
To be clear, you say:
The problem I'm running into, is that from a worker thread I'm calling
WKExteralLoader.externView!.hidden = false; WKExteralLoader.webView!.hidden = true;
You can't do UI code from a worker thread. Instead you need to write that code like this:
dispatch_async(dispatch_get_main_queue(), ^{
WKExteralLoader.externView!.hidden = false;
WKExteralLoader.webView!.hidden = true;
});
That way, from your worker thread you dispatch your UI code back to the main thread.
You may have other code that's trying to do UIKit calls from a background thread when you're not aware of it. To check, add the following line:
print("isMainThread = \(Thread.current.isMainThread)")
And then check to see what's printing in the console.
Run the hide/show code on the UI thread
dispatch_async(dispatch_get_main_queue()) {
WKExteralLoader.webView!.hidden = false;
WKExteralLoader.externView!.hidden = true;
}