Need Coroutine Download to finish first
Hi guys, maybe you can save me two days of crying. So here's my issue, I have a program that stores files server side that I need to be client side (lots of reasons for this). At times the number of files to download may be large, some times they may be small (all depends on if the server has the more recent file or not). That being said I have the download coroutine set up and the script works in so far as figuring out what files need to be downloaded, BUT, due to the way coroutines go (as if I really understand it) it doesn't finish downloading till the end of the script (which because I use variables to pass file paths means only one file eventually gets downloaded).
SO, in short I need to wait for each download to complete prior to moving on in the code. I've viewed several responses on this subject but none of them made sense to me and I was hoping someone could help guide me onto the answer.
public void CompareLists()
{
//do a lot of stuff
if (LocalFileList.Contains(ComparisonText))
{
//do some more sorting
if (LocalFileDate <= AWSFileDate)
{
StartCoroutine( WaitForRequest());
Debug.Log("last");
}
}
else
{
StartCoroutine(WaitForRequest());
Debug.Log("last");
}
}
}
}
IEnumerator WaitForRequest()
{
//do some junk here like define url and filepath
WWW www = new WWW(filepath));
yield return www;
File.WriteAllText(filepath,www.text);
}
So like I said this whole thing works EXCEPT I need one coroutine to finish before moving on, I deleted a lot of fluff and logic to make the code string shorter. Thanks for your time.
Just an update, while I'm at the mercy of getting this posted. I put a coroutine on the main thread that checks if the downloads finished. However this hasn't worked for me because Unity still process code downstream from the download requests (The methods downstream use the downloads to create prefabbed tiles, so no downloads means no tiles).
Have you tried this approach
private bool updatingFiles = false; // are we in the process of updating files?
public void CompareLists()
{
if (!updatingFiles)
{
StartCoroutine( UpdateFiles());
}
}
IEnumerator WaitForRequest()
{
updatingFiles = true;
for (int i = 0; i < LocalFileList.Count; i++)
{
// is the current file to be downloaded/updated
if (LocalFileList[i].fileNeedToBeUpdated)
{
//do some junk here like define url and filepath
// string filepath = whatever;
WWW www = new WWW(filepath));
File.WriteAllText(filepath, www.text);
while (www.progress < 1.0f)
yield return www;
}
yield return null;
}
yield return null;
updatingFiles = false;
}
In hindsight I can see that its really important to mention that the file name is being pulled by a foreach(string fileanddate in whateverlist). So that means several coroutines are being created in parallel, but the key variable fileanddate is being updated. That being said the method you proposed wouldn't work for me because it would simply skip downloading a file as it iterates through the list. Thanks for the reply though.
The only solution I can think of is a way to download files without it being a background operation or a way to hold the program till the coroutine finishes.
Or I could start doing work around 3 if theres a way to make a coroutine store an instance of the variable (Probably didn't use that term right, kind of like saying corticosteroid in a medical conversation).
<=== Noob may or may not have the answer to this. After doing a lot of reading one line started to stand out. yield return gives control back to unity. $$anonymous$$oving yield return null to the end of the coroutine statement got me some good errors of not done downloading yet, with some helpful while != wait command suggestions. This may solve my problem.
Answer by Icewing726 · Sep 06, 2016 at 01:50 AM
Here's how you wait for a download. 1) instead of following every example out there that says to yield return www right after calling WWW www, put yield return null at the end. 2) Add the code: while(!www.isDone){} so that it waits for the isdone to do anything with the information you just downloaded. (probably want to add a timeout for this)
Feedback is still welcomed, I'm a noob afterall.
Your answer