www class blocks script execution

Ok, maybe this is a silly question:
We are loading some files from disk and use www class for that und my colleague has used WWW class for that, but WITHOUT putting it into a coroutine. Instead he put a simple while Loop behind it, to wait until it ended (so the end is awaited in the current frame, where the loading was started).

  public static bool IsValidWWW(WWW www) {
      
        while (!www.isDone)
               ;
        return www != null && string.IsNullOrEmpty(www.error) && www.size > 0;
    }

The files are very small, so that doesn’t make any problem and everything works fine in Editor, on Android and Standalone, but on iOS the device is blocked.

My Guess is, that www class uses a special amount of time in the frame for downloading, then stops and the next frame is loaded. On the other platforms it extends the time for download, but on iOS the Frame is clamped to 30 so the time es up, but the execution can’t go on, because it is trapped in a while loop.

Or is www executed thread based?

I just want to be sure, how it works - because if this is the cause of the error, we have to change A LOT OF code, to make this coroutines based.

It’s surprising that this works for you as often as it does, (I even tested because I didn’t believe it!) but I’m guessing the reason is threading. It is likely the case that the WWW operation can be offloaded to another thread on some platforms, but on iOS it needs to use the main thread, at least to start the operation.

In general however, you should never do this! I tested again in the editor with my internet disconnected, and sure enough the editor froze then crashed, because it was stuck in the loop.

There’s a reason why every language’s net APIs use Async methods and callbacks, and you will likely have to redesign along this pattern. To ‘fix’ your method you could write it like this:

public static IEnumerator IsValidWWW(WWW www, Action<bool> callback) {         
         while (!www.isDone)
                yield return null;
         callback( www != null && string.IsNullOrEmpty(www.error) && www.size > 0 );
     }

There’s really no way around it, because there’s never a guarantee a net operation can complete within any fixed timeframe. Especially on mobile, where weird stuff can happen in the OS and your WWW might not return for a few seconds.

Edit: i must have misread about loading local data via Www class. While that should work, the fact that you see this behavior makes me think to avoid it. Maybe something about the file:// scheme is operating on ios in a way that requires the frame to conclude before it completes (while other platforms dont)

The .net file io classes should be able to handle any of your needs though. It might be as simple as

if (File.Exists (path))
text = File.ReadAllText(path);