• 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 w$$anonymous$$ch can be 30-40MB large containing some data. T$$anonymous$$s JSON object contains a base64 string w$$anonymous$$ch can be decoded into binary data of any type. After decoding, I want to saved t$$anonymous$$s binary data as a file on the local mac$$anonymous$$ne. 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 t$$anonymous$$s 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);
         w$$anonymous$$le(!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 w$$anonymous$$le (1-2 sec)
         print("Output...");        
         //Do somet$$anonymous$$ng 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 aw$$anonymous$$le for a www with byte count 37205271, when t$$anonymous$$s 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. T$$anonymous$$s 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, w$$anonymous$$ch 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Ios problem with www class 2 Answers

Changing multiple UI.Image source image downloaded via WWW class 0 Answers

suspend a function time to recover a data C# 2 Answers

yield return WWW stops Coroutine? 0 Answers

Coroutine needed here? 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