Search Unity

UnityWebRequest keeps caching

Discussion in 'Connected Games' started by Tomas_Kok, Feb 27, 2018.

  1. Tomas_Kok

    Tomas_Kok

    Joined:
    Oct 5, 2014
    Posts:
    12
    I have a simple authentication system in which I use UnityWebRequest objects to send a username and password and receive a registration token (which I store locally). On the server I have a .NET Core Web API and a MySQL database, which function as they should when I test them with Curl or Postman but not with Unity.

    Sometimes Unity receives a HTTP 200 response and a token when the server is down and sometimes Unity cannot find the server when it is up and running. My guess is that Unity caches the requests. Indeed, when I debug the server and send a request with Unity, it receives nothing. Adding a random number to my request does not fix it however. Adding "Cache-Control":"no-caching" to the header also does not change anything.

    I cannot find anything in the docs regarding caching of UnityWebRequest or cookies in Unity. Is there a way to turn it off or is there an alternative to using UnityWebRequest?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,212
    What platform are you on and which Unity version?
    UnityWebRequest does not cache anything on it's own, but the underlying implementation on particular platform can for example cache cookies.
     
    Tomas_Kok likes this.
  3. Tomas_Kok

    Tomas_Kok

    Joined:
    Oct 5, 2014
    Posts:
    12
    Apologies for my late reply. The problem was that I was testing it on two different platforms (desktop and mobile) and used two different servers, so actually I had two different problems. On the desktop, my .NET server settings were causing the caching and on mobile it was iOS. This gave me the false assumption that Unity was caching. I found this out after checking everything again thank's to your confirmation that UnityWebRequest should not cache anything on its own.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,212
    I've just recently found out that iOS does cache on it's own (the backend we use for UnityWebRequest does cache). This will be fixed.
     
    nilsdr and SmartMediaNL like this.
  5. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    185
    I encounter a problem, if i download a file quite often, eventually i keep getting an old version. App built using 2017.1.2, can this be the cause? If so, how to fix?
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,212
    In XCode project find WWWConnection.mm files. In it find method named "allocRequestForHTTPMethod" and add this line before return:
    [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
     
    nilsdr likes this.
  7. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    185
    Thanks! Has this been fixed in the latest 2018 versions?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,212
    Yes.
     
    DarekRusin and nilsdr like this.
  9. msf-eng92

    msf-eng92

    Joined:
    Apr 9, 2015
    Posts:
    3
    Hi, I have a similar problem with the Android platform:
    I have a script called ServerCommunicator and every script that need to communicate with server initialize it( by AddComponent), and after the job finished the ServerCommunicator will destroy...

    in the beginning, three communications will run in the Start, but when the fourth WS will run( by user request), the script will pass one of the old answers (of other webServices).

    the confusing part is in Editor everything works great and there is no platform-specific code in the project, so the code is correct, but android is not Ok...
     
    Last edited: Jan 15, 2019
  10. msf-eng92

    msf-eng92

    Joined:
    Apr 9, 2015
    Posts:
    3
    after testing everything, I finally solved my problem by changing the value of unityWebRequest.useHttpContinue to false ( which is default true)

    what is that anyway?
    why it had different behavior in the editor?
     
    mohs3n71 likes this.
  11. mohs3n71

    mohs3n71

    Joined:
    Mar 2, 2019
    Posts:
    1
    Thanks god ! I was thinking I'm the only one in the world that has the same exact problem !
    you saved my life man ! This caching problem in android was driving me crazy
     
    msf-eng92 likes this.
  12. DarekRusin

    DarekRusin

    Joined:
    Nov 15, 2013
    Posts:
    25
    We've recently switched to Unity 2018.4.0 and when running on an Amazon Fire device I'm still seeing the content being cached (seems to be depending on HTTP content expiration time).

    @Aurimas-Cernius when you said it's fixed in 2018.x, did you mean for iOS only, or in general? Or is this an Amazon specific issue?
     
  13. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,212
    I'm only aware of this issue in iOS and it was fixed. I haven't heard of this issue on Android.
     
  14. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    177
    hi, am facing same issue, please give me the fix.
     
  15. Leoniciux

    Leoniciux

    Joined:
    Aug 14, 2013
    Posts:
    40