Search Unity

Multiple UnityWebRequest behaving really weirdly on iOS

Discussion in 'Scripting' started by jariwake, Nov 28, 2018.

  1. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    I am downloading multiple files (images, videos assetbundle files, etc) simultaneously from Amazon S3 bucket using UnityWebRequests. I do this by just iterating through a List<string> of MyAsset objects (with file urls) and sending a new UnityWebRequest to the file url. I also attach a DownloadHandlerFile to each request.

    Now, for some reason some of the downloads (like 2 of 8) never start. The downloadedBytes stays at zero and the requests never finish. I am not sure if this is caused by my code, Unity, iOS or even S3.

    Weirdest part is that if I inverse the order of the list of downloaded urls, I do not get the problem, all assets get downloaded without problems. In the editor the problem does not occur either.

    Here is my code:

    Code (CSharp):
    1. public void DownloadAssets(List<MyAsset> assetsToDownload)
    2. {
    3.     foreach (var asset in assetsToDownload)
    4.     {
    5.         StartCoroutine(DownloadAsset(asset));
    6.     }
    7. }  
    8.  
    9. private IEnumerator DownloadAsset(MyAsset asset)
    10. {                    
    11.     using (UnityWebRequest request = UnityWebRequest.Get(asset.url))
    12.     {
    13.         DownloadHandlerFile dlHandler = new DownloadHandlerFile(asset.localPath);
    14.         dlHandler.removeFileOnAbort = true;
    15.         request.downloadHandler = dlHandler;
    16.            
    17.         Debug.Log("Start downloading file: " + asset.filename);
    18.        
    19.         yield return request.SendWebRequest();
    20.                        
    21.         if (request.isNetworkError)
    22.         {
    23.             Debug.LogError("Network error");                  
    24.         }
    25.         else if (request.isHttpError)
    26.         {
    27.             Debug.LogError("Http error");
    28.            
    29.         }
    30.     }
    31. }
    32.                
    33. [Serializable]
    34. public class MyAsset
    35. {  
    36.     public string filename;
    37.     public string url;
    38.     public string localPath;
    39. }
    Any tips on how to investigate this weird issue are welcome.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    That's weird indeed.
    The most straight forward place to investigate this is in the trampoline. Export XCode project and debug it. UnityWebRequest leads to calls in WWWConnection.mm (or UnityWebRequest.mm on newest version of Unity), placing breakpoints in various callbacks in delegate might help to find, what's happening. Also check the output log, maybe it has some error.
     
  3. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    Thanks for the quick answer @Aurimas-Cernius. Unfortunately I have no idea what "the trampoline" is or how to use it..Im guessing its some kind of debugger for XCode? Google results dont really help: https://www.theiphonewiki.com/wiki/Trampoline =P

    So..could you please elaborate a bit?

    And By "export XCode project" do you simply mean making an iOS build in Unity (which results in an XCode project) or something else? Also, where can I find UnityWebRequest.mm? Under the classes folder in the built project I do not see it (or the WWWConnection.mm).

    Im using Unity 2018.1.0f2.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Trampoline is a set of Unity source files that are copied to XCode project and are available for modification (at own risk, so you have to know what you are doing).
    Do iOS build, open XCode project and look under Classes/Unity folder.
     
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    If it has something to do with trampolines, you should see an error in the console "Ran out of trampolines of type X". If you see this error (where X is either 0, 1 or 2), you can set it in the iOS Player Settings inside Unity under
    Other Settings -> AOT Compilation Options
    .
    here is what you have to type in (when you want to increase to a number of trampolines to - let's say 4096):
    * Type 0:
    ntrampolines=4096

    * Type 1:
    nrgctx-trampolines=4096

    * Type 2:
    nimt-trampolines=4096


    If you need to set the number of more than one type, concatenate all instructions with a comma (and nothing else).

    However...

    I don't think that it has something to do with trampolines. They are needed for generic types and other "magic" with types in the code (actually not quiet sure what and which type is for what. But I think Generics and lambda expressions are using trampolines most).

    I think that it probably has to do with some iOS specific sh*t which hinders the OS to do too many requests at a time (I believe I experienced something similar a long time ago).
    So I guess the solution would be to allow only a certain amount of parallel requests. So, you have a queue of requests and send the first - let's say 10 request. As soon as you receive a response a slot is freed and the next request in the queue is sent automatically...
     
  6. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    For what its worth, we download dozens of files from s3 buckets simultaneously on iOS using the same calls to unitywebrequest you make, and never see issues. This is in production code across lots of different devices/ios versions.
     
  7. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    I did not see any trampoline related errors in the Xcode console.

    I got rid of the problem by implementing a download queue as @Hosnkobf suggested. I am now allowing only 4 concurrent downloads.

    So I guess it remains a mystery why some of the download UWR:s never received any response on iOS..
     
    Hosnkobf likes this.
  8. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
  9. jpgordon00

    jpgordon00

    Joined:
    Jan 9, 2021
    Posts:
    25
    Not sure why you are having issues downloading multiple files on iOS.

    My code allows for the downloading of multiple files and is tested and works properly on iOS.