2015-12-29 37 views
0

我有一對來自ParseObject,DbRound和DbLocation的類。 DbRound有一個名爲「位置」的字段,指向一個DbLocation。我創建一個查詢幷包含(「位置」),希望在獲取DbRound時獲取引用的DbLocation;沒有快樂。但是,我可以在後續的讀取操作中獲取位置。ParseQuery.Include()實際上是如何工作的?

我的目標是在單個查詢中同時獲得DbRound和引用的DbLocation。我誤解了ParseQuery.Include()的意圖,還是錯誤地使用它?或者我是否完全用錯誤的方式去討論這個問題?

這運行在統一。下面是相關的代碼:

IEnumerator CheckRound(string roundId) 
{ 
    // DbRound.Location is a pointer to DbLocation (a ParseObject derivative) 
    var query = new ParseQuery<DbRound>(); 
    query.Include("Location"); 

    var task = query.GetAsync(roundId); 

    while(!task.IsCompleted) yield return new WaitForEndOfFrame(); 

    if(! task.IsFaulted) 
    { 
     // task.Result.Location contains a default-constructed DbLocation 
     Debug.Log("task.Result.Location.IsDataAvailable: " + 
        task.Result.Location.IsDataAvailable); 

     var rel = new List<ParseObject> { task.Result.Location }; 
     var t = ParseObject.FetchAllIfNeededAsync(rel); 
     while(! t.IsCompleted) yield return new WaitForEndOfFrame(); 

     // now task.Result.Location has a value of the referenced DbLocation 
     Debug.Log("task.Result.Location.IsDataAvailable: " + 
        task.Result.Location.IsDataAvailable); 

     DisplayRound(task.Result); 
    } 
} 

日誌的結果:

12/28/2015 18:30:00.4214 task.Result.Location.IsDataAvailable: False 
12/28/2015 18:30:10.7530 task.Result.Location.IsDataAvailable: True 

TIA, --Joshua

[編輯:每個請求,添加源]

首先,哪些驅動器演示:

IEnumerator SetUpBugDemo() 
    { 
     var location = new DbLocation("A desolate place"); 
     yield return StartCoroutine(Store(location)); 

     var round = new DbRound(location); 
     yield return StartCoroutine(Store(round)); 

     yield return StartCoroutine(CheckRound(round.ObjectId)); 
    } 

    IEnumerator Store(DbLocation location) 
    { 
     var task = location.SaveAsync(); 

     while(!task.IsCompleted) { yield return new WaitForEndOfFrame(); } 
    } 

    IEnumerator Store(DbBogusRound round) 
    { 
     var task = round.SaveAsync(); 

     while(!task.IsCompleted) { yield return new WaitForEndOfFrame(); } 
    } 

而現在所導出的parseObject聲明:

[ParseClassName("Location")] 
public class DbLocation : ParseObject 
{ 
    [ParseFieldName("Text")] 
    public string Text 
    { 
     get { return GetProperty<string>("Text"); } 
     set { SetProperty<string>(value, "Text"); } 
    } 

    [ParseFieldName("HashTag")] 
    public string HashTag 
    { 
     get { return GetProperty<string>("HashTag"); } 
     set { SetProperty<string>(value, "HashTag"); } 
    } 

    [ParseFieldName("BreakdownValue")] 
    public int BreakdownValue 
    { 
     get { return GetProperty<int>("BreakdownValue"); } 
     set { SetProperty<int>(value, "BreakdownValue"); } 
    } 

    [ParseFieldName("CreationValue")] 
    public int CreationValue 
    { 
     get { return GetProperty<int>("CreationValue"); } 
     set { SetProperty<int>(value, "CreationValue"); } 
    } 

    public DbLocation(string text) 
    { 
     Text = text; 
     HashTag = "<none>"; 
     BreakdownValue = 0; 
     CreationValue = 0; 
    } 

    public DbLocation() 
    { /* required for Parse */} 
} 

[ParseClassName("Round")] 
public class DbRound : ParseObject 
{ 
    [ParseFieldName("Location")] 
    public DbLocation Location 
    { 
     get { return GetProperty<DbLocation>("Location"); } 
     set 
     { 
      var locationRef = 
       ParseObject.CreateWithoutData<DbLocation>(value.ObjectId); 
      SetProperty(locationRef, "Location"); 
     } 
    } 

    public DbRound(DbLocation location) 
    { 
     Location = location; 
    } 

    public DbRound() 
    { /* required for Parse */ } 
} 

[編輯:添加解析API控制檯輸出]

有一個從上述樣本代碼產生圓的一個實例和位置。

GET classes/Round 
RESPONSE 
{ 
    "results": [ 
     { 
      "Location": { 
       "__type": "Pointer", 
       "className": "Location", 
       "objectId": "YuJlyxhRSe" 
      }, 
      "createdAt": "2015-12-29T22:53:59.966Z", 
      "objectId": "ugg61jLPN6", 
      "updatedAt": "2015-12-29T22:53:59.966Z" 
     } 
    ] 
} 


GET classes/Location 
RESPONSE 
{ 
    "results": [ 
     { 
      "BreakdownValue": 0, 
      "CreationValue": 0, 
      "HashTag": "<none>", 
      "Text": "A desolate place", 
      "createdAt": "2015-12-29T22:53:59.319Z", 
      "objectId": "YuJlyxhRSe", 
      "updatedAt": "2015-12-29T22:53:59.319Z" 
     } 
    ] 
} 
+0

您對include()的期望是正確的。查詢看起來很好。你如何證明自己「不快樂」?發佈一個查詢獲取DbRound結果以及日誌輸出的例子,該例子說明不返回指向的位置對象。 – danh

+0

好吧,我編輯了示例以記錄IsDataAvailable狀態。此外,我一直在通過調試器中的代碼觀察這個步驟。 – jrl

+0

我們可以看看物體本身嗎? – danh

回答

0

嘿,我想出了這個問題。 ParseQuery.Include()不會改變查詢對象;它會創建一個添加Include()的新實例。所以我需要的是:

var query = new ParseQuery<DbRound>(); 
query = query.Include("Location"); 

然後查詢按預期返回位置數據。