Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 677
Jaka Jančar Avatar asked Sep 16 '09 08:09

Jaka Jančar


1 Answers

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)

like image 101
rpetrich Avatar answered Oct 05 '22 05:10

rpetrich