-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];

    });
    });
Paige DePol
  • 1,121
  • 1
  • 9
  • 23
Kartik
  • 1
  • 1

1 Answers1

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.

random
  • 8,568
  • 12
  • 50
  • 85
  • How would this stop the gui from hanging? – Gruntcakes May 06 '14 at 19:31
  • 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. http://stackoverflow.com/questions/11593900/uiwebview-stringbyevaluatingjavascriptfromstring-hangs-on-ios5-0-5-1-when-called – random May 06 '14 at 19:43
  • 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 – Gruntcakes May 06 '14 at 19:47
  • 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?? – random May 06 '14 at 19:52
  • Out of curiosity did you try changing it to `performSelectorOnMainThread` ?? I'm curious if it did anything at all – random May 06 '14 at 19:58
  • couldnt able to resolve with any of them, Actually i am updating Highcharts in the HTML,which are in UIWebView,while updating the highchart dynamically, adding point one by on,e the GUI freezes for a while when ever added one point. – Kartik May 08 '14 at 02:27