I have been stuck with this exact same bug for quite a while too.
Just like you did I tried pretty much everything, from NSOperationQueue's addOperationWithBlock:
to dispatch_async
to performSelectorOnMainThread:withObject:waitUntilDone:
... (admittedly shooting in the dark)
Then I figured that I was always calling my js code using [myJsContext evaluateScript:jsString];
, and getting the JSContext from the UIWebView using [myWebView valueForKeyPath: @"documentView.webView.mainFrame.javaScriptContext"];
.
Instead I tried the older [myWebView stringByEvaluatingJavaScriptFromString:jsString];
, and it seems to work now (at least it hasn't crashed in a long time).
So maybe that's a solution, as you're using UIWebViews as well? My understanding is that evaluateScript:
is especially useful if you're using a JSContext without a UIWebView.
(This answer might lack actual technical certainties, so please dig in if you know more about JavaScriptCore... But hey, it seems to just work for me!)