当UIWebView尝试阻塞锁定Web线程时

当UIWebView尝试阻塞锁定Web线程时

本文介绍了当UIWebView尝试阻塞锁定Web线程时,避免主线程冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有UIWebView共享一个网络线程。

All UIWebViews share a single web thread.

当其中一个是 init superview等,他们将尝试以阻塞的方式从主线程锁定web线程,从而暂时冻结主线程的运行循环。

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.

如果web线程正忙,例如同时做一个长同步 XMLHttpRequest ,这可能会阻塞主线程很长时间。

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

以避免这种情况?

如果我可以修改UIWebView,我只是使锁尝试非阻塞,但显然不是这样,所以我在寻找其他聪明的想法。

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.

推荐答案

它的长短短是:避免做任何阻碍web线程大量时间( window.alert window.prompt XMLHttpRequest.open('GET' url,false),可能还有其他)

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)

此外,避免调用锁定Web线程的方法,然后立即执行需要很长时间因为一旦控制返回到运行循环,web线程才被解锁。 (例如:调用 - [UITextView setText:] 然后在主线程上同步读取文件)

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)

这篇关于当UIWebView尝试阻塞锁定Web线程时,避免主线程冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:17