【问题标题】:How to upload nested model information to firestore in Xamarin.iOS如何将嵌套模型信息上传到 Xamarin.iOS 中的 Firestore
【发布时间】:2022-01-11 00:32:08
【问题描述】:

我正在使用 Xamarin.Firebase.iOS.Firestore 包在 Xamarin.Forms 中工作,并且我有一个类似的嵌套模型

public class Grow
{
    public string Id { get; set; }
    public string PlantName { get; set; }
    public List<Day> Days { get; set; }
}
public class Day
{
    public string Id { get; set; }
    public string GrowId { get; set; }
    public string LightsOn { get; set; }
    public string LightsOff { get; set; }
    public List<Nutrient> Nutrients { get; set; }
}
public class Nutrient
{
    public string Id { get; set; }
    public string DayId { get; set; }
    public string NutrientName { get; set; }
    public double NutrientAmount { get; set; }
}

我不确定如何在 Xamarin.iOS 中构建此内容以进行上传,我想将其作为字典上传。我应该使用Dictionary&lt;object, object&gt; 吗?以及如何将其转换为键和值的 iOS 特定代码?我是 Xamarin 的新手,如果这似乎是一个初学者问题,我很抱歉! 我试过的是

public async Task<bool> InsertGrow(Grow grow)
{
    try
    {
        var keys = new NSObject[]
        {
            new NSString("plantName"),
            new NSObject(new NSString[] {
                new NSString("lightsOn"),
                new NSString("lightsOff"),
                new NSObject(new NSString[] {
                    new NSString("nutrientName"),
                    new NSString("nutrientAmount")
                })
           })
        }
    }
}

对于键,但我认为这不是正确的方法,将每个模型视为单独的字典会更好吗?

【问题讨论】:

    标签: c# google-cloud-firestore xamarin.forms


    【解决方案1】:
    1. 在使用 Xamarin Forms 时,我不会存储 iOS 特定对象。只需存储常规的 c# 字符串和对象。

    1. 我找不到任何有用的“嵌套对象”到/从 C# 示例,所以开始一个“社区”答案,其中包含一些可能让您开始的链接。

    Dotnet firestore sample.

    展示了一些有用的技巧。特别注意属性[FirestoreProperty] 放置应存储的属性。但是不显示 NESTED 对象。

    以上示例来自Add Data to Cloud Firestore

    浏览那些官方的 Google 文档。请注意它们如何显示不同语言的代码。在每个代码 sn-p 的右侧是More 下拉菜单。 c# 在该下拉列表中。


    miyu's Plugin.CloudFirestore - see section "Data-Mapping".

    我已经看到有人提到这使得从 C# 访问 firestore 变得更容易。

    Handling Nested Objects in firestore with flutter.

    这不是 C#,它的颤振(Dart 语言)。如果有人为 C# 写了一个类似的博客,那就太好了。我提到它是因为它讨论了在 firestore 中嵌套对象所涉及的原则。


    android - how to save data in xamarin firestore.

    因为这两行而包含这个:

    resp = mDatabase.Child(username + "/Phone");
    resp.SetValue(phone.Text);
    

    这基本上是存储“键”和相应的“值”所需要的。在 Firestore 中建立“字典”的一种方法。在这种情况下,键是username + "/Phone",值是phone.Text

    【讨论】:

    • 非常感谢!我最终选择了 Plugin.Firestore,我至少可以看到我的索引页面和我的测试数据库条目。仍在努力让我的详细视图正常工作,但我确实可以在我的应用中使用我的用户帐户访问数据库。
    猜你喜欢
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多