如何从CloudCode调用返回ParseObject数组

时间:2014-07-28 23:19:19

标签: unity3d parse-platform

有一个解析的CloudCode函数,如下所示:

    Parse.Cloud.define("getCurrentEvents", function(request, response) {
    var TimedEvent = Parse.Object.extend("TimedEvent");
    var query = new Parse.Query(TimedEvent);
    query.greaterThan("expiresOn", new Date());
    query.find({
        success: function(results) {
            response.success(results);
        },
        error: function(error) {
            response.error("There was an error while looking for TimedEvents");
        }
    });
});

它返回一个TimedEvent数组,如下面的curl测试所示:

{"result":[{"expiresOn":{"__type":"Date","iso":"2014-07-31T22:31:00.000Z"},"playMode":"Normal","tableId":"Carnival","objectId":"J1LSO3EnKi","createdAt":"2014-07-28T21:48:22.983Z","updatedAt":"2014-07-28T22:32:14.304Z","__type":"Object","className":"TimedEvent"}]}

但是当尝试从Unity SDK访问它时,我得到一个“无法转换为目标类型”的例外,其中包含以下行:

System.Threading.Tasks.Task<Parse.ParseObject[]> task = 
               Parse.ParseCloud.CallFunctionAsync<Parse.ParseObject[]> ("getCurrentEvents", parameters);

我也试过

System.Threading.Tasks.Task<IEnumerable<Parse.ParseObject>> task = 
               Parse.ParseCloud.CallFunctionAsync<IEnumerable<Parse.ParseObject[]>> ("getCurrentEvents", parameters);

具有相同(缺乏)的结果。 SDK期望什么样的签名?

1 个答案:

答案 0 :(得分:0)

你有没有试过这样的东西(没有IEnumerable?):

Threading.Tasks.Task<Parse.ParseObject> task = Parse.ParseCloud.CallFunctionAsync<Parse.ParseObject>("getCurrentEvents", parameters);

但更好的是,您可以扩展ParseObject以在Unity中创建自己的TimedEvent类,如下所示:

[ParseClassName("TimeEvent")]
public class TimeEvent : ParseObject
{
    [ParseFieldName("expiresOn")]
    public DateTime expiresOn
    {
        get { return GetProperty<DateTime>("expiresOn"); }
        set { SetProperty(value, "expiresOn"); }
    }

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

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

    // any other fields you want to access
}

然后您可以像这样查询您的数据:

IEnumerator getTimedEvents(Dictionary<string, object> parameters)
{
    var cloudTask = Parse.ParseCloud.CallFunctionAsync<TimeEvent>("getCurrentEvents", parameters);
    while (!cloudTask.IsCompleted)
        yield return null;

    if (cloudTask.IsCanceled || cloudTask.IsFaulted)
    {
        // handle error
    }
    else
    {
        TimeEvent t = cloudTask.Result;
        // do stuff with t
    }
}

P.S。不要忘记在某个地方注册你的Parse类(我通常在早期GameObject的Awake()中注册)。在你的情况下,你会这样做:

Parse.ParseObject.RegisterSubclass<TimedEvent>();