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): WWW www = new WWW(uri); while(!www.isDone){ Debug.Log(www.progress); } if(www.error != null www.error.Length > 0) { Debug.Log("There was an error getting the file - " + www.error); } else { xml = www.text; Debug.Log(xml); } I have already checked that the device is connected to the network and can get the file through Safari. Any ideas? Thanks.
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
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?
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.
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?
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!
"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): protected void BlockingSend(WWW www) { while( !www.isDone ) { // block and do nothing } Application.Quit(); } Then not only will the Application.Quit() never be reached on iOS, but the WWW will also not be sent.