Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice

WWW.isDone block on iOS

Discussion in 'Scripting' started by jojoblaze, Sep 26, 2011.

  1. jojoblaze

    jojoblaze

    Joined:
    Aug 2, 2010
    Posts:
    19
    Hi guys, I'm writing a script for download a file from Internet, but I need to do that in synchronous mode, blocking the script execution until the file is downloaded.

    This snippet of code works fine in the editor, but on iOS the file can't be downloaded and progress is always zero.

    Code (csharp):
    1.  
    2.         WWW www = new WWW(uri);
    3.        
    4.         while(!www.isDone){
    5.                    Debug.Log(www.progress);
    6.                 }
    7.        
    8.         if(www.error != null  www.error.Length > 0)
    9.             {
    10.             Debug.Log("There was an error getting the file - " + www.error);
    11.         } else {
    12.             xml = www.text;
    13.         Debug.Log(xml);
    14.         }
    15.  
    I have already checked that the device is connected to the network and can get the file through Safari.
    Any ideas?

    Thanks.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    there is nothing you can do.

    WWW runs on NSURL connections on iOS (or generally the OS native http / https pipe) which don't provide a progress, they are fired off async and when they are done they are done.

    So what you want to do is firing off a 'downloading state' after WWW, then use yield www; and then fire off the download done state
     
  3. jojoblaze

    jojoblaze

    Joined:
    Aug 2, 2010
    Posts:
    19
    Hi Dreamora, thank you for your reply.
    No what I want is a synchronous download that block the script execution until download is finished.
    Using yield statement script execution doesn't stop and polling isDone property doesn't work as shown before (it became an infinite loop).

    Any suggest?
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    What do you mean with block in detail?
    That the rest of the script is not executed until the download is done or that the whole app is frozen?

    1. Rest of script not executed

    Then your code or a yield www; do it. If they don't do it then there is a bug somewhere else as that code works fine with reachable servers to talk to

    infinite loop sounds like it worked and that the download just never terminated.


    2. Whole app is not meant to execute

    Thats significantly more complex, but you can use states to "fake" it a "its halted" state for example.
     
  5. jojoblaze

    jojoblaze

    Joined:
    Aug 2, 2010
    Posts:
    19
    With "block" I mean the game must download that file before, because it is the configuration file for the whole game and it cannot continue without it.
    (The app doesn't freeze.)
    My code doesn't work on iOS (you can try deploying it on a Apple device).
    For use yield statement I need to use it in a coroutine, but the focus of a coroutine is avoid script blocking.

    I don't think there is a bug somewhere...I have created a blank project with only one script just to test this problem

    How do you would do that?
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Now I see where the prob comes from: Never, no matter what, try to use WWW outside coroutines, really never.
    Especially not with what you do there.

    The problem is: If you don't have yield, the WWW thread will finish but the result will never be processed at all as you leave no cpu time for it. Simple as that.

    Your problem is trivial to solve:

    1. use WWW in a coroutine and introduce a download scene
    2. Check if file is present, if not
    2.1 Check connectivity
    2.2 Do the download and store the file
    3. Switch into the real scene and use the file


    just be aware: this will not go anywhere on iOS. Apple does not accept any application that work like this, 'android style' (where its needed to overcome the download limitation) as the app store has no size restriction. Either the game works after installing without wifi or you get rejected flat out.
    if the idea is to offer a downloader and get around the 20mb limit then you have totally missed the point of the 20mb limit which is to limit the major costs of phone line traffic and which is the reason the limit exists!
     
  7. TamaHobbit

    TamaHobbit

    Joined:
    May 18, 2014
    Posts:
    10
    "If you don't have yield, the WWW thread will finish but the result will never be processed at all as you leave no cpu time for it. Simple as that."

    That's not true. If you write:
    Code (CSharp):
    1. protected void BlockingSend(WWW www)
    2.     {
    3.         while( !www.isDone )
    4.         {
    5.             // block and do nothing
    6.         }
    7.  
    8.         Application.Quit();
    9.     }
    Then not only will the Application.Quit() never be reached on iOS, but the WWW will also not be sent.
     
unityunity