0

So I have an app that's using Parse. I'm able to create new ParseObjects with no problem, as well as query for saved ParseObjects. However, whenever I try to update an existing ParseObject I've queried for, I get the following error:

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

Now I've found some people getting similar errors, with "get_bundleIdentifier" being the difference, and I think it has to do with not being run from the "main thread", but I don't know how to affect that. I've tried the same code in a coroutine as suggested here and I get the same error.

I'm using Parse Version 1.6.0 and Unity Pro 5.1.2. Here is my code:

using UnityEngine;
using System.Collections;
using Parse;
using System.Threading.Tasks;
public class SimpleParseScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        var query = ParseObject.GetQuery("ChallengeScores") // query to parse to find scores and whos turn
            .WhereEqualTo("challengeID", 20307271925313064);
        query.FirstAsync().ContinueWith(t =>
                                        {
            ParseObject gameStatus = t.Result;
            print (gameStatus.ObjectId); // print queried ParseObject ObjectId - works fine
            gameStatus["turnsTaken"] = 100;
            Task saveTask = gameStatus.SaveAsync();
        });
    }
}

Now, if I comment out "gameStatus["turnsTaken"] = 100;", then try to save it, it runs through just fine, though nothing gets saved for obvious reasons.

Anyone got a clue? The parse documentation right here says it should be as simple as declaring it and saving it again after querying, but no go.

Thank you for your time and patience.

Community
  • 1
  • 1
VKnai
  • 33
  • 5

1 Answers1

0

So I figured out a solution; I thought you could save values on the fly within a query, but I guess saving information to the variable should happen outside of the query.

What I have working now looks something like this:

    using Parse;
    using System.Threading.Tasks;

    public class MainMenuScript : MonoBehaviour {

    ParseObject parseObjToUpdate; 
    private bool hasUpdatedParse;
    public string myCurrentChalID;

    void Start()
    {
        hasUpdatedParse = false;
        var query = ParseObject.GetQuery("ChallengeStatus")
            .WhereEqualTo("challengeID", myCurrentChalID);
            query.FirstAsync().ContinueWith(t =>
             {
                parseObjToUpdate = t.Result;
                print (parseObjToUpdate.ObjectId); // to debug that something is happening and it's the right object
              });
    }

    void Update ()
    {
       if (parseObjToUpdate != null && !hasUpdatedParse)
        {
            parseObjToUpdate["playerTurn"] = PlayerPrefs.GetString("opponentID");
            parseObjToUpdate.SaveAsync();
            hasUpdatedParse = true;
        }
    }

So yeah. Gonna mark this as solved. Thank you everyone watching, it's been great.

VKnai
  • 33
  • 5