Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fisyher84 · Mar 12, 2012 at 04:01 AM · textcoroutinewwwasync

Why does accessing www.text blocks the coroutine using WWW class?

I have written the following co-routine that send a JSON post request to webservice that also return a JSON object which can be 30-40MB large containing some data. This JSON object contains a base64 string which can be decoded into binary data of any type. After decoding, I want to saved this binary data as a file on the local machine. I want to make the whole process as asynchronous as possible and so far the download progress from WWW.progress is working fine but I encounter a strange block when accessing WWW.text:

 IEnumerator WWWJSONPost(string url, string jsonString)
     {        
                 
         var encoding = new System.Text.ASCIIEncoding();
         byte[] formData = encoding.GetBytes(jsonString);
         
         //Create the headers for this POST
         Hashtable theHeaders = new Hashtable();
         print("Request Headers:");
         theHeaders.Add("Content-Type", "application/json");
         print("Content-Type,application/json");
         theHeaders.Add("Content-Length", formData.Length);
         print("Content-Type,"+formData.Length);
         theHeaders.Add("Accept", "application/json");
         print("Accept,application/json");
         
         string userpwd = user + ":" + password;
         string encodedUserpwd = Base64Codec.EncodeTo64(userpwd);
         theHeaders.Add("Authorization", "Basic " + encodedUserpwd);
         print("Authorization," + "Basic " + encodedUserpwd);
         //++++++    
         
         //The blocking WWW object that initiate request upon construction
         WWW theWWW = new WWW(url, formData , theHeaders);
         while(!theWWW.isDone)
         {
             //Report progress
             if(m_ProgressHandler != null)
             {
                 m_ProgressHandler(theWWW.progress);//Works
             }
             yield return null;
         }
         
         yield return theWWW;

 //When www.text is access, all following codes are blocked for a while (1-2 sec)
         print("Output...");        
         //Do something with received data
         print("WWW byte count: " + theWWW.bytes.Length);            
         if(theWWW.error != null)
         {
             print("Error: " + theWWW.error);
             yield break;
         }
         
         print("Deserializing...");
         byte[] WWWBytes = theWWW.bytes;//Very fast, does not block
         string JSONString = theWWW.text;//blocks for awhile for a www with byte count 37205271, when this line is commented, no blocking occurs
         yield return null;
     }

Initially, I thought that creating another coroutine to decode the www.text would work but it did not. Now I'm pretty much stuck here. Is there anyway to prevent the block or should I just allow it to happen?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rabbit-Stew-dio · Mar 12, 2012 at 10:03 PM

You are trying to convert 37MB of binary data into a string. This is going to be expensive. If you can, use the byte-array or use a BinaryReader to convert your string in smaller chunks so that you can 'yield' in between.

If you make the assumption that your binary data is all ASCII, you could also write out the byte-array manually, using a StringBuilder and a simple cast from byte to char.

Personally, I would strongly suggest you move away from JSON for binary bulk transfers. At the moment you are allocating first a byte-array, then a string, then you parse that string into some new byte-array, which you then write. (And you don't even parse yet - add the JSON parser and the base-64 decoder and you have another waiting time added here.) Skip the parsing non-sense and let your server return binary data for these requests.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fisyher84 · Mar 14, 2012 at 08:53 AM 0
Share

I would do so if I have any control over what the server returns. I will try to suggest to the programmer-in-charge to do as you have suggested.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my coroutine skipping a method? 1 Answer

How to load scene when async.progress is 0.9? 0 Answers

In Unity 4.1 www.isDone doesn't seem to work in Android. Any suggestion? 0 Answers

Set string.ToCharArray() to the first letter 1 Answer

Can you run a coroutine more often than Update? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges