return from a for loop in a Coroutine ?

private IEnumerator Start() {
        List<string> contents = server.GetContents();

        DirectoryInfo dir = new DirectoryInfo("some directory");
        FileInfo[] files = dir.GetFiles("some pattern");


        for (int i = 0; i < contents.Count; i++) {
            SaveFileInfo s = Instantiate(save, scrollViewContent).GetComponent<SaveFileInfo>();
            for (int j = 0; j < files.Length; j++) {
                if (files[i].Name == contents[i]) {
                    GetFileLocaly(files[i].FullName);
                    return;
                }
            }
            StartCoroutine(GetFileFromServer());
.
.
.

I want to return(do another cycle) from the second loop if a contition is met, however coroutines do not allow to “return”, only to pause.

How do I skip getting file from the server if it already exists locally ?

Thanks

Try this to exit your coroutine.

yield break;

return means your exiting your function , brake means your exiting your loop, but continue your function.

@andymads yield break; stops everything

Sorry, my mistake, I thought that’s what you were asking.

Maybe you should try with async instead?
using System.Threading.Tasks;

When the code gets to this part:

 if (files[i].Name == contents[i]) {
                    GetFileLocaly(files[i].FullName);
                    return;
                }

I want to move to the next item in the outer loop, in normal function this would be possible with simple “return” no ?

No, return will exit a method. You need to use the break keyword.

Ok found the probelem…

       for (int i = 0; i < contents.Count; i++) {
            SaveFileInfo s = Instantiate(save, scrollViewContent).GetComponent<SaveFileInfo>();
            for (int j = 0; j < files.Length; j++) {
                if (files[j].Name == contents[i]) {
                    GetFileLocaly(files[j].FullName);
                    return;
                }
            }

Silly me, I was using “i” for both files where one was suppoed to be “j” , also I added break and a boolean condition to do the rest of the function, that fixed it for me.

Thanks everyone