Avoid main thread freezes when UIWebView tries to blockingly lock the web thread

戏子无情 提交于 2020-01-22 07:43:12

问题


All UIWebViews share a single web thread.

When one of them is init-ed, removed from superview etc., they will attempt to lock the web thread from the main thread in a blocking fashion, thus temporarily freezing the run loop of the main thread.

If the web thread is busy, e.g. while doing a long synchronous XMLHttpRequest, this may block the main thread for a long time.

Is there a way to avoid this?

If I could modify UIWebView, I'd just make the lock attempt non-blocking, but obviously that's not the case, so I'm looking for other clever ideas.


回答1:


The long and short of it is: avoid doing anything that blocks the web thread for a significant amount of time (window.alert, window.prompt, XMLHttpRequest.open('GET', url, false), possibly others)

Also, avoid calling methods that lock the web thread and then immediately doing something that takes a long time as the web thread is only unlocked once control is returned to the run loop. (Example: call -[UITextView setText:] then read a file synchronously on the main thread)




回答2:


you cannot make synchronous xhr since this will lead to crossdomain policy errors.



来源:https://stackoverflow.com/questions/1431594/avoid-main-thread-freezes-when-uiwebview-tries-to-blockingly-lock-the-web-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!