Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What thread does the completionHandler of WKWebView's evaluateJavascript method run on?

I am trying to figure out if it is safe to manipulate UI objects in the completion handler of WKWebView's - evaluateJavaScript:completionHandler:. The docs do not seem to specify.

like image 357
weiyin Avatar asked Mar 12 '15 17:03

weiyin


People also ask

What does EvaluateJavaScript do?

EvaluateJavaScript(NSString, WKJavascriptEvaluationResult)Evaluates the given JavaScript string.

What is WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


2 Answers

https://developer.apple.com/reference/webkit/wkwebview/1415017-evaluatejavascript?language=objc

The completion handler always runs on the main thread.

Almost at the very end of the page. Possibly added sometime after you posted this question.

like image 86
WiseOldDuck Avatar answered Oct 05 '22 04:10

WiseOldDuck


It does not matter. If it is not specified then you should assume that it is not.

You can very easily run your UI code on the main thread using:

dispatch_async(dispatch_get_main_queue(), ^{
    // Your UI code here
});

Better safe than sorry.

like image 41
Stefan Arentz Avatar answered Oct 05 '22 02:10

Stefan Arentz