A Workaround for WWW yield bug on iOS

Quick Update

This issue is resolved in Unity 5.1.3 patch 2 (download here). This post only deals with the scenario where you cannot upgrade to a newer version (due to various project constraints).

This post presents a simple workaround for overcoming a serious www yield bug in some of the latest versions of Unity. The issue occurs on iOS when running the following code from inside a coroutine:

www bugIn case you’re looking for the digest, you can grab the solution (workaround) for this issue here:  https://gist.github.com/liortal53/280998ff87141980b98a

The Bug

The issue occurs when using Unity’s WWW class inside of a coroutine (which is a very common practice). The WWW object is used together the yield statement, to block execution until the download completes. In case there’s no network connectivity, this call hangs indefinitely and will not return (the coroutine will never complete !)

Solution

The solution is to use a wrapper class (WWWRequest) that wraps a WWW instance. This object is used in a coroutine (just like WWW) with a very minor syntax change,  and it will not hang due to the bug.

In order to use it, convert the following code used in a coroutine:

WWW www = new WWW(url);
yield return www;

into the following code (note how we’re wrapping the WWW instance inside a WWWRequest):

WWW www = new WWW(url);
yield return StartCoroutine(new WWWRequest(www));

Note how this code is similar to the previous version, but it will not hang indefinitely due to the bug, and instead will timeout correctly if there’s no network connectivity.

Conclusion

I have reported this issue to the nice dudes over @ Unity and they mentioned that this is going to be fixed in the next patch release (Unity 5.1.3 patch 2).

If you can’t wait for the next patch release, please use the solution presented in this post.

 

This entry was posted in Unity and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *