【问题标题】:Firestore.Query.GetSnapshotAsync() never returns and doesn't give any errors - the program just doesn't stopFirestore.Query.GetSnapshotAsync() 永远不会返回并且不会给出任何错误 - 程序只是不会停止
【发布时间】:2020-06-29 13:51:04
【问题描述】:

我正在 Unity 版本 2019.4.1f1 (LTS) 中构建游戏,并且我正在使用 Firebase Firestore 数据库。
基本上当我尝试从数据库中获取用户时,特别是当我调用GetSnapshotAsync() 时,它会卡住。
层次结构如下:
StartState 调用FetchUserState,后者调用DBGetObjectState,后者与DatabaseManager 通信(DatabaseManager 包含调用GetSnapshotAsync() 的行)。

如果有人能帮我解决这个问题,那就太好了。 感谢您的帮助!

FetchUserState

//fetch user by userId
            await stateManager.PushTempState(new DBGetObjectState<T>(StringConstants.DB_USERS_COLLECTION, //("Users")
                field: "UserId", fieldEqualTo: _userId, withLoading: false));

DBGetObjectState

Log("Getting Documents");
var documents = await DatabaseManager.GetDocuments<T>(_path, _limit, _orderBy, _orderByDescending, _field, _fieldEqualTo);
Log("Got something, checking..."); // doesn't get here

DatabaseManager

public static async Task<Collection<T>> GetDocuments<T>(string path, int limit = -1,
        string orderBy = null, string orderByDescending = null, string field = null, object fieldEqualTo = null)
    {
        var collectionRef = DBUtills.GetCollectionRef(path);

        Query query = collectionRef;

        if (field != null && fieldEqualTo != null)
            query = query.WhereEqualTo(field, fieldEqualTo);
        if (orderBy != null)
            query = query.OrderBy(orderBy);
        if (orderByDescending != null)
            query = query.OrderByDescending(orderByDescending);
        if (limit >= 0)
            query = query.Limit(limit);

        Debug.Log("Snapshotting!");
        QuerySnapshot collectionSnapshot = await query.GetSnapshotAsync();
        Debug.Log("got a snapshot, checking..."); // doesn't get here
        
        //....
    }

【问题讨论】:

  • 你确定你的回调是在 Unity 主线程上执行的吗?不如试试ContinueWithOnMainThread
  • @derHugo 感谢您的帮助,您完全正确,就是这样。猜猜我会从现在开始使用它。
  • 请在帖子中添加解决方案作为答案,这将有助于社区成员检查此问题的解决方案。

标签: c# firebase unity3d google-cloud-firestore


【解决方案1】:

感谢任何试图提供帮助的人,显然这是我用来处理任务的插件,我应该在问问题之前先检查一下。 无论如何,如果有人仍然遇到问题,@derHugo 的回答绝对是一个解决方案,而不是等待任务,只需使用 ContinueWithOnMainThread() 创建一个将在任务完成后调用的回调,如下所示:

query.GetSnapshotAsync().ContinueWithOnMainThread(task =>
{
   if(task.IsFaulted)
   {
       //....
   }
   QuerySnapshot collectionSnapshot = task.Result;
   //....
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2020-10-24
    • 2012-08-19
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多