是否可以将 WebView 的 UI 和 HTTP 线程分开? [英] Is it possible to separate WebView's UI and HTTP threads?

查看:26
本文介绍了是否可以将 WebView 的 UI 和 HTTP 线程分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于各种原因,我需要.调试器中的一个快速步骤告诉我,在 UI 线程(又名 main)上调用的 loadUrl() 完成速度太快,无法处理连接在 UI 线程上同步.(我在调用 loadUrl() 之前和之后放置了一个断点,其中的 URL 我知道不会很快提供).

所以,我的回答是你已经完成了......他们已经分开了!(是的!)

For various reasons, I need to runOnUiThread() the actual instantiation & initialization of WebView.

Which means that its underlying HTTP connections are also made on the UI thread?

If this is true, is it possible to separate the WebView's UI thead from the HTTP connections thread?

If it is possible, what is the proper way of accomplishing this?

解决方案

I find it very hard to believe that Android would run remote HTTP requests on the UI thread, assuming you initiate the requests via WebView.loadUrl(). This would make for a terrible user experience.

Just quickly, I ran an Android app in the debugger, with a basic WebView, and stopped in the debugger. Here's what I see:

If WebViewCoreThread, WebViewWorkerThread, or http0 - http3 aren't handling the network connections for WebView objects, then

  1. They have very bad names
  2. Android is built really badly

Also, if you look at this answer by one of stack overflow's highest reputation users, loadUrl() is asynchronous. A quick step through in the debugger tells me that loadUrl(), which is called on the UI thread (aka main), completes way too fast for the connection to be handled synchronously on the UI thread. (I put a breakpoint before and after the call to loadUrl() with a URL that I know isn't served very quickly).

So, my answer is you're done ... they're already separate! (yeah!)

这篇关于是否可以将 WebView 的 UI 和 HTTP 线程分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆