【问题标题】:Get a collection from Firestore in Unity?从 Unity 中的 Firestore 获取集合?
【发布时间】:2020-03-24 13:51:17
【问题描述】:

我正在尝试通过关注this link 在 Unity 中获取 Firestore 集合,但它给了我一个错误:

“任务”不包含“文档”的定义,并且没有可访问的扩展方法“文档”接受“任务”类型的第一个参数。

我的代码:

CollectionReference allCitiesQuery = db.Collection("MyList");
Task<QuerySnapshot> allCitiesQuerySnapshot = allCitiesQuery.GetSnapshotAsync();

foreach (DocumentSnapshot documentSnapshot in allCitiesQuerySnapshot.Documents)
      {           
          Dictionary<string, object> city = documentSnapshot.ToDictionary();
          foreach (KeyValuePair<string, object> pair in city)
          {
            Debug.Log(pair.Key + " " + pair.Value);
            //Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
          }
      }
    }

我已阅读文档,但很少关注 Unity,因为几天前它刚刚实现。谁能帮帮我好吗?

【问题讨论】:

    标签: firebase unity3d google-cloud-firestore


    【解决方案1】:

    尝试改变

    CollectionReference allCitiesQuery = db.Collection("MyList");
    Task<QuerySnapshot> allCitiesQuerySnapshot = allCitiesQuery.GetSnapshotAsync();
    

    进入

    Query allCitiesQuery = db.Collection("MyList");
    QuerySnapshot allCitiesQuerySnapshot = await allCitiesQuery.GetSnapshotAsync();
    

    并确保您已包含以下所有内容:

    using Firebase;
    using Firebase.Firestore;
    using Firebase.Extensions;
    

    【讨论】:

    • 这是正确的,通常你有一个任务并且必须在task.IsComplete时得到task.Result。 Async/await 并不是唯一的方法(而且并不总是最好的)。我在这里将其分解:firebase.googleblog.com/2019/07/…,但基本上您的其他选择是使用 ContinueWithOnMainThread 或字面上等待(通常在协程中使用 WaitUntil )让 IsComplete 变为 true 并拉出 Result。
    • 感谢您的评论! Philipp 我再次实现了“等待”,并阅读了this 参考,现在它可以工作了。我将代码留在响应中,以防其他用户需要它。
    • Pux0r3,我以后一定会看的,因为我想改进我的代码,谢谢。
    【解决方案2】:

    这里是有相关问题的人的功能代码。

    using Firebase;
    using Firebase.Extensions;
    using Firebase.Firestore;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Example  : MonoBehaviour
    {
    
    public  async void GetMyCollections()
        {
          Query allCitiesQuery = db.Collection("MyRootCollection");
          QuerySnapshot allCitiesQuerySnapshot = await allCitiesQuery.GetSnapshotAsync();
    
          foreach (DocumentSnapshot documentSnapshot in allCitiesQuerySnapshot.Documents)
          {
              Dictionary<string, object> city = documentSnapshot.ToDictionary();
              foreach (KeyValuePair<string, object> pair in city)
              {
                Debug.Log(pair.Key + " " + pair.Value);
              }
          }
        }
    }
    

    参考:FirebaseDocsAsync programming

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2018-03-24
      • 2018-03-28
      • 2018-04-04
      • 1970-01-01
      • 2019-02-17
      相关资源
      最近更新 更多