Firebase 从Unity的Firestore获得一个收藏?

Firebase 从Unity的Firestore获得一个收藏?,firebase,unity3d,google-cloud-firestore,Firebase,Unity3d,Google Cloud Firestore,我试图通过以下方式获得Unity中的firestore集合,但它给了我一个错误: “Task”不包含“Documents”的定义,并且没有可访问的扩展方法“Documents”接受类型为“Task”的第一个参数 我的代码: CollectionReference allCitiesQuery = db.Collection("MyList"); Task<QuerySnapshot> allCitiesQuerySnapshot = allCitiesQuery.GetSnapsho

我试图通过以下方式获得Unity中的firestore集合,但它给了我一个错误:

“Task”不包含“Documents”的定义,并且没有可访问的扩展方法“Documents”接受类型为“Task”的第一个参数

我的代码:

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);
          }
      }
    }
CollectionReference allCitiesQuery=db.Collection(“MyList”);
Task allCitiesQuerySnapshot=allCitiesQuery.GetSnapshotAsync();
foreach(DocumentSnapshot DocumentSnapshot在allCitiesQuerySnapshot.Documents中)
{           
Dictionary city=documentSnapshot.ToDictionary();
foreach(城市中的KeyValuePair对)
{
Debug.Log(pair.Key+“”+pair.Value);
//WriteLine(“{0}:{1}”,pair.Key,pair.Value);
}
}
}
我已经阅读了文档,但是很少关注Unity,因为几天前它刚刚实现。有人能帮我吗?

试试换衣服

CollectionReference allCitiesQuery = db.Collection("MyList");
Task<QuerySnapshot> allCitiesQuerySnapshot = allCitiesQuery.GetSnapshotAsync();
并确保包括以下所有内容:

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

下面是针对有相关问题的用户的功能代码

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);
          }
      }
    }
}
使用Firebase;
使用Firebase.Extensions;
使用Firebase.Firestore;
使用系统线程;
使用System.Threading.Tasks;
公共课示例:单一行为
{
公共异步void GetMyCollections()
{
Query allCitiesQuery=db.Collection(“MyRootCollection”);
QuerySnapshot allCitiesQuerySnapshot=等待allCitiesQuery.GetSnapshotAsync();
foreach(DocumentSnapshot DocumentSnapshot在allCitiesQuerySnapshot.Documents中)
{
Dictionary city=documentSnapshot.ToDictionary();
foreach(城市中的KeyValuePair对)
{
Debug.Log(pair.Key+“”+pair.Value);
}
}
}
}

参考资料:,。

这是正确的,通常您有一个任务,必须获得该任务。当Task.IsComplete时,会得出结果。Async/await并不是实现这一点的唯一方法(也不总是最好的)。我在这里将其分解为:,但基本上您的其他选项是使用ContinueWithOnMainThread或直接等待(通常在协同程序中使用WaitUntil),以便IsComplete变为true并提取结果。感谢您的评论!Philipp我再次实现了“等待”,我阅读了参考资料,现在它可以工作了。我将代码留在响应中,以防其他用户需要。Pux0r3,我以后一定会阅读它,因为我想改进我的代码,谢谢。
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);
          }
      }
    }
}