parse.com(Unity)中的关系查询

时间:2014-04-23 03:51:31

标签: unity3d parse-platform

我在parse.com上找到了例子。我有2个对象:Post和Comment,在Comment对象中有一个collumn:“父”指向Post obj的指针,我想加入它们:

var query = ParseObject.GetQuery ("Comment");
// Include the post data with each comment
query = query.Include("parent");
query.FindAsync().ContinueWith(t => {
    IEnumerable<ParseObject> comments = t.Result;
        // Comments now contains the last ten comments, and the "post" field
        // contains an object that has already been fetched.  For example:
    foreach (var comment in comments)
    {

        // This does not require a network access.
        string o= comment.Get<string>("content");
        Debug.Log(o);
        try {
            string post = comment.Get<ParseObject>("parent").Get<string>("title");
            Debug.Log(post);


        } catch (Exception ex) {
            Debug.Log(ex);  
        }
    }

});

有效! 然后,我有2个对象:Usercore和Gamescore,在Gamescore对象中有一个collumn:“playerName”指向Post obj的指针我也希望加入它们:

var query = ParseObject.GetQuery ("GameScore");
        query.Include ("playerName");
        query.FindAsync ().ContinueWith (t =>{
            IEnumerable<ParseObject> result = t.Result;
            foreach (var item in result) {
                Debug.Log("List score: ");
                int score = item.Get<int>("score");
                Debug.Log(score);
                try {
                    var obj = item.Get<ParseUser>("playerName");
                    string name = obj.Get<string>("profile");
                    //string name = item.Get<ParseUser>("playerName").Get<string>("profile");

                    Debug.Log(name);

                } catch (Exception ex) {
                    Debug.Log(ex);  
                }
            }
        });

但它不起作用,请帮助我!

3 个答案:

答案 0 :(得分:1)

为什么你没有像第一个例子那样做以下事情:

query = query.Include ("playerName");

你刚才 -

query.Include ("playerName");

答案 1 :(得分:0)

一种解决方案是确保正确获取ParseUser对象。即:

var obj = item.Get<ParseUser>("playerName");
Task t = obj.FetchIfNeededAsync();
while (!t.IsCompleted) yield return null;

然后你可以这样做而不用担心:

string name = obj.Get<string>("profile");

但那将是对Parse的另一个潜在要求,这是不幸的。似乎query.Include ("playerName")在Parse的Unity版本中没有正常工作?

答案 2 :(得分:0)

我相信您应该为此使用多级包含,例如第一次查询中的.Include("parent.playerName")