如何使用 .NET MAUI 在 Firebase 中捕获产品节点 ID?

问题描述 投票:0回答:1

我正在使用 firebase-database-dotnet 包在 .NET MAUI 中使用 Firebase,并且需要帮助捕获从 Firebase 检索的产品的节点密钥 (ID)。

Firebase中的数据结构如下:

products

  product1
    category: "dairy"
    description: "Sliced Arequipe cheese, ideal for accompanying your meals."
    name: "Arequipe Cheese Sliced"
    price: 10.99

  product10
    category: "dairy"
    description: "Box of 30 Kikes eggs."
    name: "Kikes Eggs 30 pcs"
    price: 3.99

我有以下方法从 Firebase 检索产品:

 public async Task<IEnumerable<T>> GetItemsAsync(bool forceRefresh = false)
 {
     try
     {
         var firebaseObjects = await _query
             .OnceAsync<T>();

         return firebaseObjects
             .Select(x => x.Object);// Devuelve los FirebaseObject<T> completos
     }
     catch (Exception ex)
     {
         return null;
     }
 }

此方法返回产品对象,但我不知道如何捕获节点键(product1、product10等)。

现在,我想实现一种在Firebase中保存所选产品的方法,我需要捕获该产品对应的节点ID。例如:

[RelayCommand]
public async Task SaveProduct(Product product)
{
    // Here, I want to use the GetItemsAsync method to retrieve the existing products.
    // How can I capture the node ID (such as 'product1' or 'product10') of the selected product?
}

是否可以在表中包含 ID,或者这被认为是不好的做法?如何捕获节点键以及当前数据中的数据

maui
1个回答
0
投票

一般来说,

product1
product10
等。你提到的都是自动生成的。

要更新

products
列表上的特殊项目,我们通常添加一个字段并保证其值是唯一的。

例如:

 public class Product
{
    public string Name { get; set; }
    
    //add field ID (it's value is unique)
    public string ID { get; set; }
}

那么如果我们想要更新

products
列表的特殊产品,我们可以先迭代产品列表,通过字段
ID
找到该项目。

© www.soinside.com 2019 - 2024. All rights reserved.