Unity 2019.4 LTS中Firestore.GetSnapshotAsync()无响应求助
GetSnapshotAsync() Blocking in Unity 2019.4 LTS Hey there, let's tackle this frustrating blocking issue with GetSnapshotAsync() in your Unity + Firestore setup. I've dealt with similar async quirks in Unity before, so here are targeted fixes and checks to try out:
1. Fix Async Context Conflicts with ConfigureAwait(false)
Unity's synchronization context can sometimes hang async operations when they try to jump back to the main thread. Try modifying your await call to avoid forcing a context switch:
QuerySnapshot collectionSnapshot = await query.GetSnapshotAsync().ConfigureAwait(false);
This tells the async method not to require returning to the original thread (your state machine's context), which often resolves deadlocks in nested async calls like your state system's chain.
2. Ensure Firebase is Fully Initialized
It's easy to overlook that Firebase might not be ready when you fire off your database call. Add a dependency check early in your flow, either in DatabaseManager.GetDocuments or your game's initialization phase:
// Add this at the start of GetDocuments<T> if (FirebaseApp.DefaultInstance == null) { var dependencyStatus = await FirebaseApp.CheckAndFixDependenciesAsync(); if (dependencyStatus != DependencyStatus.Available) { Debug.LogError($"Firebase dependencies failed: {dependencyStatus}"); return null; // Or handle error appropriately } }
Wait for Firebase to finish setting up before attempting any Firestore operations.
3. Validate Query Conditions & Firestore Rules
Your WhereEqualTo("UserId", _userId) query could be causing silent hangs if:
- The
UserIdvalue doesn't exist in the collection (though this should usually return an empty snapshot, not hang) - Firestore security rules block access to the collection
Test with a simplified query first (remove the WhereEqualTo clause) to see if you get a snapshot back. Also, temporarily loosen your Firestore rules in the console for testing:
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; // Only use for testing! } } }
If this fixes the issue, tighten the rules back up once you confirm access works.
4. Check for State Machine Deadlocks
Your state system (StartState → FetchUserState → DBGetObjectState) might be blocking the async task from executing. If PushTempState is a synchronous method that holds execution, try wrapping the async call in Task.Run() to offload it:
// In FetchUserState await Task.Run(() => stateManager.PushTempState(new DBGetObjectState<T>( StringConstants.DB_USERS_COLLECTION, field: "UserId", fieldEqualTo: _userId, withLoading: false )));
Also verify that your StateManager isn't holding a lock or stuck in a loop that prevents the async task from progressing.
5. Enable Firebase Debug Logs
Turn on Firebase's debug logging to get more visibility into what's happening during the snapshot call:
// Add this in your game's initialization code Firebase.FirebaseApp.LogLevel = Firebase.LogLevel.Debug;
Check the Unity Console for detailed logs about the Firestore request—this will tell you if the request is stuck waiting for network, hitting an auth issue, or failing silently.
6. Verify Firebase SDK Compatibility
Unity 2019.4 LTS works best with older, stable Firebase SDK versions (e.g., 8.x series—newer SDKs drop support for Unity 2019). If you're using a very old or very new SDK, try switching to a version explicitly listed as compatible with Unity 2019 in Firebase's release notes.
内容的提问来源于stack exchange,提问作者Happinessess

