Search Unity

Reusing a DownloadHandlerScript

Discussion in 'Scripting' started by geniusz, Jun 14, 2018.

  1. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    Code (CSharp):
    1. UnityWebRequest req = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST, downloadHandler, null);
    2. req.disposeDownloadHandlerOnDispose = false;
    3. yield return req.Send();
    4. print(downloadHandler.text);
    When downloadHandler is a DownloadHandlerBuffer
    the code works well each time called.

    When downloadHandler is a DownloadHandlerScript
    first time it works well.
    second time the request never returns after send.

    Is there any difference between DownloadHandlerBuffer and DownloadHandlerScript?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Download handlers aren't meant to be reused. What is the purpose of reusing?
    If request never completes, it doesn't sound good, it is possibly a bug. Can you report it?
     
  3. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    Just want to avoid create too many objects.
    and I have reported it.
     
  4. Wisearn

    Wisearn

    Joined:
    Jul 10, 2012
    Posts:
    8
    I'd like to point out that I also got stuck on what I thought to be the supposed behaviour where I could have my class that deals with sending and receiving data be the target of the receiving data information with the added bonus of not having to make a new class every call.

    For now knowing this just isn't possible the code is not going to be very easily maintained with lots of nested delegates...
     
  5. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    @Aurimas-Cernius so are you telling me that
    UnityEngine.Networking.UnityWebRequest cannot be reused? That I have to destroy it, garbage-collect it, and create a new one for every new web request I make even if it's the same one? And keep fragmenting the non-consolidating memory of Unity with every new object? Who made/allowed this class/behavior and how come they got a job at unity? :)
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    No, UnityWebRequest objects are not reusable. You have to create a new one every time.

    A request would inevitably allocate and then deallocate memory, since incoming data needs to be stored somewhere, HTTP headers parsed etc.
     
  7. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    Yes, they need to be stored somewhere, in a buffer that you don't destroy and have some idea/control about when and where it was allocated and how big. Except we can't do that by (bad) design :D
     
    zalogic and xVergilx like this.
  8. Deleted User

    Deleted User

    Guest

    Me too...
    We are sending frames continously and all that's changing with each request is the array received...
    What's the alternative to WebRequests if you want to have control over how much garbage you generate?
     
    Tudor likes this.
  9. JaviTheGreat

    JaviTheGreat

    Joined:
    Sep 28, 2015
    Posts:
    6
    @Aurimas-Cernius
    One common scenario where it would be very useful that UnityWebRequest were reusable is in a retry pattern...
    Right now we need to create the request on every try/iteration, which makes it difficult to simplify the code or make it reusable, because I'm using several UnityWebRequest constructors for different API requests.
     
    zalogic likes this.