Unity Freezes during yield return www;

These lines of code reside inside a method called getWebRequest() which is called with StartCoroutine(getWebRequest()).

Debug.Log ("1 ");
WWW www = CreateRequest (_BaseURL + "url", request);
Debug.Log ("2 ");
yield return www;
Debug.Log ("3 ");

The code prints out 1 & 2 but then freezes for 2-3 seconds before it prints out the 3 (as is expected since its doing a http request). But during this time rendering is also frozen/blocked.

As this is called inside a coroutine I thought that the main thread would not be impacted by a slow long running request, but it seems that it is.

I’m thinking of placing this entire call inside a separate Threading.Thread but as I’m new to Unity there might be a better solution to this that I dont know about?

Updated with more code details:

private WWW CreateRequest(string url, object data){

	string jsonRequest = "{}";

	if (data != null) {
		string requestData = Converter.SerializeToString (data);
		jsonRequest = "{\"Data\": \"" + requestData + "\"}";
	}

	Debug.Log ("Json Request: " + jsonRequest);
	
	byte[] dataBytes = GetBytes (jsonRequest);
	
	WWWForm form = new WWWForm();
	System.Collections.Generic.Dictionary<string,string> headers = form.headers;
	headers["Content-Type"] = "application/json";
	
	WWW www = new WWW(url, dataBytes, headers);
	return www;
}

I solved my problem by doing a WebRequest on a separate thread and then returning messages to a thread safe List for the main thread to consume. This way the web calls are not blocking the main thread and the Update() only has to deal with interpreting the response.

I made certain to only use the methods available on the WP8 because if they are on that platform then they also exist on the other platforms.

Made an example with it here: Send Post request in Unity on any platform (even Windows Phone) without WWW