using Unity, I am trying to intialize my the firestore using CheckAndFixDependenciesAsync() and getting a document from firestore using GetSnapshotAsync(). From a firebase article, it is possible to add a continuation for the task with ContinueWithOnMainThread as shown in the example here:
Debug.Log("Checking Dependencies");
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(fixTask =>
{
Assert.IsNull(fixTask.Exception);
Debug.Log("Authenticating");
var auth = FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWithOnMainThread(authTask =>
{
Assert.IsNull(authTask.Exception);
Debug.Log("Signed in!");
var successes = PlayerPrefs.GetInt("Successes", 0);
PlayerPrefs.SetInt("Successes", ++successes);
Debug.Log($"Successes: {successes}");
auth.SignOut();
Debug.Log("Signed Out");
});
});
However, this is not the case for my project, where I want to use GetSnapshotAsync() instead to retrieve some 'level' data - everything inside the block is not running.
private void Start()
{
Debug.Log("Database awake ");
//check if all dependency is in project
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
string lvCode = "oHFxRAIpIHfT8moiYSl9";
Debug.Log("Get level :" + lvCode);
DocumentReference levelref = db.Collection("level").Document(lvCode);
levelref.GetSnapshotAsync().ContinueWithOnMainThread(querySnapshotTask =>
{
//Debug.Log here is not ran
Debug.Log("Completed? " + querySnapshotTask.Result);
Debug.Log("Faulted? " + querySnapshotTask.IsFaulted);
});
});
}
Can someone explain why is this happening to my code and possibly provide some alternatives that use the ContinueWith methods? (as opposed to await and coroutine solutions)
Author of that article here ๐
I can't quite see if you're doing anything wrong, but my guess is that maybe you're doing a db = FirebaseFirestore.DefaultInstance outside of that block (ignore this if you're doing a lazy iniitalization). Calling that before CheckAndFixDependenciesAsync could be causing your issue (CheckAndFixDependenciesAsync verifies that DefaultInstance can be called without erroring out).
Subbing out your db reference might fix it:
private void Start()
{
Debug.Log("Database awake ");
//check if all dependency is in project
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
string lvCode = "oHFxRAIpIHfT8moiYSl9";
Debug.Log("Get level :" + lvCode);
DocumentReference levelref = FirebaseFirestore.DefaultInstance.Collection("level").Document(lvCode);
levelref.GetSnapshotAsync().ContinueWithOnMainThread(querySnapshotTask =>
{
//Debug.Log here is not ran
Debug.Log("Completed? " + querySnapshotTask.Result);
Debug.Log("Faulted? " + querySnapshotTask.IsFaulted);
});
});
}
Also, pay close attention to any error logging, changing the first ContinueWith to ContinueWithOnMainThread might reveal more errors (some versions of Unity do fail to report errors in background tasks).
Finally it's worth checking that you actually do have Firestore setup with your current game. Assuming you have a google-services.json or GoogleService-Info.plist in your Assets/ directory, you can go to "Window>Firebase>Documentation":

And click "Open in Console" to jump right to your project and double check that Firestore is setup:

If you do find that changing the first ContinueWith to ContinueWithOnMainThread fixes your issue completely, it could be worth filing a bug. Generally the Firebase libraries should be thread safe, and since Firestore is in beta maybe it's a use case that was missed.