ParseQuery.Include()实际上是如何工作的

本文关键字:工作 何工作 Include 实际上 ParseQuery | 更新日期: 2023-09-27 18:28:22

我有一对从ParseObject、DbRound和DbLocation派生的类。DbRound有一个名为"Location"的字段,该字段指向DbLocation。我创建了一个查询和Include("Location"),希望在获取DbRound时获得引用的DbLocation;没有快乐。但是,我可以在随后的获取操作中获取Location。

我的目标是在一个查询中获得DbRound和引用的DbLocation。我是误解了ParseQuery.Include()的意图,还是使用错误?还是我完全错了?

这在Unity运行。以下是相关代码:

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,--约书亚

[编辑:每个请求,添加源]

首先,是什么驱动了演示:

    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控制台输出]

有一个Round和Location的实例是由上面的示例代码生成的。

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"
        }
    ]
}

ParseQuery.Include()实际上是如何工作的

嘿,我发现问题了。ParseQuery.Include()不会更改查询对象;它创建了一个新实例,该实例添加了Include()。所以我需要的是:

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

然后查询按预期返回Location的数据。