I'm launching a WWW get request and upon receiving the response, I want to parse the received string into a JSON. The problem is that due to the very large size of the response, getting the response string using www.text blocks the UI.
I've tried putting the www.text method on another thread, however Unity isn't thread-safe and I get an exception when I tried to access www.text.
Are there any alternatives to getting the string from a www request?
EDIT: It's not the speed of the request that's bothering me (e.g. how long it takes to download the file) but the time it takes to get the string from the completed WWW object. I've tried doing this inside a coroutine, however I believe WWW.text is an atomic operation aka you can't do it over a number of frames.
EDIT2: Code: coroutine to start the request
sender.StartCoroutine(GetSessionWithPositions(sender, URL));
static IEnumerator GetSessionWithPositions(UI.HistoryTableViewController sender, string URL) {
WWW request = new WWW(URL);
yield return request;
isMakingARequest = false;
if (string.IsNullOrEmpty(request.error)) {
Debug.Log(Time.time.ToString());
sender.ReceivedShotsPosition(request);
} else {
Debug.Log("GetSessionWithPositionsRequest failed with error: " + request.error);
}
}
Code that launches the deserialize Thread
public void ReceivedShotsPosition(WWW request) {
Thread deserializeThread = new Thread(new ParameterizedThreadStart(Deserialize));
deserializeThread.Start((request as object));
StartCoroutine(CheckIfDone());
}
The code that handles the request
private void Deserialize(object request) {
WWW wwwObject = request as WWW;
JSONObjects.Session session = Utility.JsonHelper.DeserializeJSON<JSONObjects.Session>(wwwObject.text);
int index = 0;
dataSource = new Models.ShotDataObject[session.Shots.Length];
foreach (JSONObjects.Shot JSONShot in session.Shots) {
dataSource[index++] = new Models.ShotDataObject(JSONShot);
}
finishedSerializing = true;
}
The CheckIsDone coroutine simply checks that bool and updates the UI if it's set to true.
EDIT3: The response is a JSON string.
EDIT4: UnityThread variant
private void Deserialize(object json) {
WWW request = json as WWW;
string jsonString;
UnityThread.executeInUpdate(() => {
jsonString = request.text;
JSONObjects.Session session = Utility.JsonHelper.DeserializeJSON<JSONObjects.Session>(jsonString);
int index = 0;
dataSource = new Models.ShotDataObject[session.Shots.Length];
foreach (JSONObjects.Shot JSONShot in session.Shots) {
dataSource[index++] = new Models.ShotDataObject(JSONShot);
}
finishedSerializing = true;
}
);
}
Doesn't work as intended, same issue as before.