I'm trying to create multiple WKWebView views inside of a background process and then add them to a view on the main thread once they are all done loading.
Each of the WKWebView's contains a chart rendered via javascript so the load time takes about a second per WKWebView so I'm trying to offload the processing to the background so the UI isn't blocked.
This works fine when dispatch_get_main_queue is commented out, however the ui is blocked for 5-10 seconds. Only the brown background of the WKWebView shows up, none of the contents from the webpage.
var webViews : [WKWebView] = []
var myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(myQueue, {
for i in 0...10
{
var url : NSURL? = NSURL(string:"http://google.com")
var req = NSURLRequest(URL:url!)
var webview = WKWebView(frame:CGRectMake(0, height * CGFloat(i), width, height))
webview.loadRequest(req)
webview.backgroundColor = UIColor.brownColor()
self.webViews.append(webview)
}
dispatch_async(dispatch_get_main_queue(),{
for item in self.webViews
{
self.view.addSubview(item)
}
});
});