Skip to content

Commit 77b5263

Browse files
committed
ensure that messages are sent to the WV on the main thread
1 parent f78a99c commit 77b5263

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

WebViewJavascriptBridge/WebViewJavascriptBridge.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ - (void)_dispatchMessage:(NSDictionary *)message {
106106
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
107107
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
108108
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
109-
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]];
109+
if ([[NSThread currentThread] isMainThread]) {
110+
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]];
111+
} else {
112+
dispatch_sync(dispatch_get_main_queue(), ^{
113+
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]];
114+
});
115+
}
110116
}
111117

112118
- (void)_flushMessageQueue {

0 commit comments

Comments
 (0)