-1

I have to call this method stringByEvaluatingJavaScriptFromString every second to update the UI of an Webview. My UI gets hang for a while when ever the method gets called. The following code snippet didn't solved my problem. Thanks in advance.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    dispatch_async(dispatch_get_main_queue(), ^{

    [webView stringByEvaluatingJavaScriptFromString:string];

    });
    });
2
  • stringByEvaluatingJavaScriptFromString blocks the thread that it runs on. Commented May 6, 2014 at 19:39
  • Therefore you need to make your java script quicker, or broken down into smaller quick steps Commented May 6, 2014 at 19:56

1 Answer 1

0

Some searching around shows that wrapping in GCD block could potentially be a bug and doing this following helps:

[webView performSelectorOnMainThread:@selector(stringByEvaluatingJavaScriptFromString:) withObject:string waitUntilDone:NO];

However, stringByEvaluatingJavaScriptFromString: has to be called on the main thread which if your string variable is lengthy js there maybe no way to smooth it out from the obj-c side. You could try optimizing your js code.

Sign up to request clarification or add additional context in comments.

6 Comments

How would this stop the gui from hanging?
Depending on how complex your js code is it won't. What I saw was that there was a bug filed in wrapping the call in blocks was causing a longer UI lock. stackoverflow.com/questions/11593900/…
Useful to know. However I think the op has found the call to stringByEvalutingJavaScriptFromString was slow and so he wrapped it in dispatch_async because he thought that makes it run asynchronously and thus would stop the gui freezing. "The following code snippet didn't solved my problem" i.e. the code is an attempt at a solution to solve code that is already hanging
The problem is that stringByEvalutingJavaScriptFromString is part of UIKit which has to be called from the main thread. I saw some other hacks to try and get around it but nothing pretty. Also, I though the OP said the accepted answer fixed his problem??
Out of curiosity did you try changing it to performSelectorOnMainThread ?? I'm curious if it did anything at all
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.