Search code examples
c#unity-game-engineparse-platformngui

Parse.com retrieving count of last day sale in unity


I'm trying to show count from a parse class into label but the following error is occurring:

"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."

My code is given below. Can anyone help me?

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();
});

Solution

  • You can only send value of NGUI label from main thread. Simple solution here would be waiting until "result" variable changes and then assigning label.text. I recommend looking into Tasks, there are much more friendly ways of controlling Parse.com queries.

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

    Try this:

    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();
    
    }