GET 请求不保存数据

GET Request dont save data

我正在尝试从 GET 请求中保存一些数据。我使用 StartCoroutine 请求并使用 Lambda 表达式保存数据。

我的代码是这样的:

  Using UnityEngine;
  using System.Collections;

  public class Test : MonoBehaviour {

 // Use this for initialization
 public void Start () {
     string url1 = "http://localhost/virtualTV/query/?risorsa=";

     string ciao = "http://desktop-pqb3a65:8080/marmotta/resource/ef299b79-35f2-4942-a33b-7e4d7b7cbfb5";
     url1 = url1 + ciao;

     WWW www1 = new WWW(url1);

     var main=new JSONObject(JSONObject.Type.OBJECT);
     var final= new JSONObject(JSONObject.Type.OBJECT);;
     StartCoroutine(firstParsing((value)=>{main = value;
         final= main.Copy();
         Debug.Log(main);
     }));
     Debug.Log(final);
 }

 public IEnumerator firstParsing( System.Action<JSONObject> callback)
 {
     string url2 = "http://localhost/virtualTV/FirstQuery/?risorsa=";
     string ciao = "http://desktop-pqb3a65:8080/marmotta/resource/ef299b79-35f2-4942-a33b-7e4d7b7cbfb5";
     url2 = url2 + ciao;
     WWW www2 = new WWW(url2);
     yield return www2;
     string json = www2.text;

     //Parsing del json con creazione di un array
     var firstjson = new JSONObject(json);
     var tempVideo = new JSONObject(JSONObject.Type.OBJECT);
     var array2 = new JSONObject(JSONObject.Type.OBJECT);

             tempVideo.AddField ("id", firstjson.GetField ("id"));
             tempVideo.AddField ("type", firstjson.GetField ("type"));
             tempVideo.AddField ("url", firstjson.GetField ("url"));
             array2.Add (tempVideo);

     yield return array2;    
     callback (array2);
     Debug.Log ("First Run" + array2);

 }

当我尝试在命令后使用 FINAL 时,

 final=main.copy()

它是空的。你能帮我把值保存在变量 final 中吗?谢谢大家

协程的执行分布在许多帧中。当协程遇到 yield return 语句时,它会 returns 调用方法,完成执行,直到任务完成。


在您的例子中,Start 中的 Debug.Log(final) 语句在 firstParsing 中的 yield return www2; 执行后立即执行。尚未调用回调,这就是 final 为空的原因。

要在 final 中分配 外部 回调函数后访问该值,您必须设置一个 bool在回调中分配 final 后设置为 true。像这样:

StartCoroutine(firstParsing((value)=>{main = value;
    final= main.Copy();
    Debug.Log(main);
    isFinalAssigned = true;
}));

// In another method
if(isFinalAssigned)
{
    // Access final
}

你必须注意,上面的if 语句仅在像Update 这样周期性调用的方法中有用。如果您在只调用一次的方法(如 OnEnable)中访问 final,则必须等待 final 被分配。您可以使用另一个协程来完成此任务,例如

IEnumerator DoSomethingWithFinal()
{
    while(!isFinalAssigned)
       yield return null; // Wait for next frame
    // Do something with final
}

最简单的方法是在您的 callback.

中使用(访问)final

EDIT2:根据您的评论,您可以执行以下操作。您将使用协程,因为阻塞主游戏线程不是一个好主意。

private JSONObject final = null; // Make final a field

无论你在哪里使用 final,你都有两个选择。

  1. 使用空检查if(final == null) return;这可能不切实际。
  2. 在协程中等待final被赋值,做一些回调。这是您可以干净地做您想做的事情的唯一方法。

查看下面的实现。

// Calls callback after final has been assigned
IEnumerator WaitForFinal(System.Action callback) 
{
    while(final == null)
        yield return null; // Wait for next frame
    callback();
}

// This whole method depends on final. 
// This should be similar to your method set up if you have 
// good coding standards (not very long methods, each method does only 1 thing)
void MethodThatUsesFinal()
{
    if (final == null)
    {
        // Waits till final is assigned and calls this method again
        StartCoroutine(WaitForFinal(MethodThatUsesFinal));
        return;
    }

    // use final
}