Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

WWW Class Get Not Refreshing After First Call

Discussion in 'Editor & General Support' started by newdev1111, Jun 9, 2014.

  1. newdev1111

    newdev1111

    Joined:
    Aug 20, 2013
    Posts:
    4
    Hello all. Having a critical problem with the WWW class on iOS devices. When I WWW GET data from a web server using PHP/MySQL it retrieves the data as expected. I then update the data in the MySQL database to new values and WWW GET the data again but the updated MySQL data does not get retrieved. Unity uses the old data from the first WWW GET. Seems like Unity is not clearing out the old data and replacing with the new data. This does not happen when ran inside Unity, it works as expected.


    My C# code used to work in the past and I believe the issue might be related to newer versions of Unity but am unsure. In any event I am unable to fix it. Hopefully someone has ran into this problem. Thanks!
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Sounds like the iOS is caching the request. Either configure your server to tag the request as don't cache, or append a random url parameter so the server sees a different request each time.
     
  3. newdev1111

    newdev1111

    Joined:
    Aug 20, 2013
    Posts:
    4
    Thank for for the direction Graham. I'm not familiar with server caching so researching this now. I see that query cache type = on in the server variables and settings section in phpMyAdmin. The mySQL docs say that when a table is updated the cache is automatically refreshed... so the docs information contradicts what's happening to me.

    Do you think this is the cause even though everything works properly when ran in the Unity editor on my Mac? Seems like since it works on Unity Editor/Mac the server side stuff is configured properly.

    Another interesting point is that I have an older version of my app (exported from Unity about 6 months ago) running on Android. It uses the same WWW code and works correctly. Seems like something changed in Unity and that the server side is still working properly?
     
    Last edited: Jun 9, 2014
  4. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I suspect the editor and iOS device use different ways to handle caching. If you are certain your server is configured correctly, then use use a url parameter to vary the URL that you pass. Also, I'm not sure how the mySQL docs can describe how your web server handles caching. It's possible the mySQL docs are talking about a cache that sits between the database and the php library.
     
  5. newdev1111

    newdev1111

    Joined:
    Aug 20, 2013
    Posts:
    4
    I think your right about iOS caching differently than the editor. My web server (hosted by godaddy) is working correctly. The mySQL docs were referring to the mySQL cache not the actual server.

    I ran a test in Unity with two GUI buttons. Each button uses a different URL path to a duplicate PHP script. I only changed the name on the duplicate PHP script. Each button/URL works exactly one time. It retrieves the correct updated table data only once. I also ran a test with a button that changed the URL string in Unity alternating between the two URL’s. The result was the same.

    The WWW data doesn't go away when you quit the app or shutdown/startup the iOS device. It's staying in memory somehow. The only way to retrieve new data is to delete the app from the device and re-install it.

    This didn't happen in earlier versions of Unity. Is this a bug? Seems like you would always want freshly updated data from the server.

    In my case I'm retrieving data such as balance, leaderboard data, ranking, etc. I need it to get new data every time I request it. I would need an infinite number of PHP scripts (one for each time I get the data using WWW) to be able to use a new URL in Unity each time. Like I would need the PHP scripts generated dynamically on the server and then a matching URL generated in Unity?
     
    Last edited: Jun 10, 2014
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,625
    you should read the docs before going that far in your conclusions
    https://developer.apple.com/library...//apple_ref/doc/c_ref/NSURLRequestCachePolicy
    by default iOS will cache results of your request based on URL alone
    there are two possibilities:
    1. add "cookie" just to change your URL everytime (like counter)
    2. change caching behaviour
    Starting with Unity 4.3 we moved the ios-specific WWW code to trampoline so you can tweak connection the way you want

    i attached the quick sample i did that shows how to tweak native connection. Use it as the base for your stuff
     

    Attached Files:

    doggan and Graham-Dunnett like this.
  7. newdev1111

    newdev1111

    Joined:
    Aug 20, 2013
    Posts:
    4
    It works. Thank you for taking the time to write the custom connection. I appreciate it very much. I wouldn't have been able to do that myself. If I saw those docs I wouldn't have understood. This is all far beyond my level of knowledge. I was speculating based on what I know. Last time I used WWW in previous versions of Unity it worked as needed by default without a custom connection plugin. Thanks again.
     
  8. Bastiuscha

    Bastiuscha

    Joined:
    Mar 1, 2014
    Posts:
    21
    Hey.... i know, this thread is really old, but it's the only one, with my problem, i did found. Until today, with iOS is the same problem. This plugin seems to be don't work anymore. I can't do a build with xcode.... what i can do?
    How i can realize "add "cookie" just to change your URL everytime (like counter)" ? Should i do an "/" ++ every request? So i will not produce data trash?!
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,737
    We will release a fix for it soon. As a workaround you can modify WWWConnection.mm file in exported XCode project and disable caching on NSURLConnection.
     
  10. Bastiuscha

    Bastiuscha

    Joined:
    Mar 1, 2014
    Posts:
    21
    I'm really a xcode noob, is it possible, that you can do a screenshot or something, there i can find this setting? Would be great.
     
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,737
    It's not a setting, it's a code line you need to write.
    Open WWWConnection.mm file and add one line of code:

    [request setAllHTTPHeaderFields: headers]; <--- this line should exist, find it and add the next one
    [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
     
  12. Bastiuscha

    Bastiuscha

    Joined:
    Mar 1, 2014
    Posts:
    21
    WORKS! Thanks a lot!!! Yes please write a fix, could be nice. For the moment, i'm happy.
     
    Aurimas-Cernius likes this.