Parse.com 统一检索最后一天的销售计数

Parse.com retrieving count of last day sale in unity

我正在尝试将解析 class 中的计数显示到标签中,但出现以下错误:

"CompareBaseObjectsInternal can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function."

我的代码如下。谁能帮帮我?

ParseQuery<ParseObject> USQuery = ParseObject.GetQuery ("Sales")
    .WhereEqualTo ("transactionType", "Purchase")
    .WhereGreaterThan ("createdAt",DateTime.Now.AddDays(-1));

USQuery.CountAsync().ContinueWith(t =>
{
    int result=t.Result;
    labelUSSale.text=result.ToString();
});

您只能从主线程发送 NGUI 标签的值。这里的简单解决方案是等到 "result" 变量更改然后分配 label.text。我建议查看任务,有更友好的方式来控制 Parse.com 查询。

https://parse.com/docs/unity_guide#tasks

试试这个:

IEnumerator GetSales()
{   
    int result = -1;

    ParseQuery<ParseObject> USQuery = ParseObject.GetQuery ("Sales").WhereEqualTo ("transactionType", "Purchase").WhereGreaterThan ("createdAt",DateTime.Now.AddDays(-1));

    USQuery.CountAsync().ContinueWith(t =>
    {
        result=t.Result;  
    });

    while (result == -1) yield return new WaitForSenOfFrame();
    labelUSSale.text=result.ToString();

}