0

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();
});
Steven
  • 166,672
  • 24
  • 332
  • 435
  • Where is this code? The error is suggesting that you have it in a constructor and that instead you should move it to either the Start or Awake methods? – Dover8 Feb 16 '15 at 09:59

1 Answers1

0

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

}
Greg Lukosek
  • 1,774
  • 20
  • 40