You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2019.4 LTS中Firestore.GetSnapshotAsync()无响应求助

Troubleshooting Firebase Firestore 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 UserId value 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:07:33