【问题标题】:Query Firebase object where object contains userID查询对象包含用户 ID 的 Firebase 对象
【发布时间】:2016-08-17 17:53:29
【问题描述】:

我有一个包含产品列表的 Firebase 数据库,每个产品都有一个购物车元素的子列表。我只想接收包含具有正确用户 ID 的购物车元素的产品元素。

我正在使用 FirebaseUI 列表适配器。我得到了产品列表的所有元素,但我只想显示包含用户 ID 的产品。

我可以从 ListAdapter 中删除视图吗?或者可以按一些查询排序?

欢迎所有解决问题的想法!

编辑

这是产品类的一部分。

public class Product implements Serializable{

    private boolean active;

    private ArrayList<Cart> carts;

    private String description;

    private String details;

    private String ean;

    private String id;

    private String image;

    private String name;

    private Float price;

    private ArrayList<ProductPack> productpacks;

    private int stock;

    private int stockavailable;

    private Float tax;

    private String users; 
}

firebase中存储的部分数据:

products
     -KG8O_5iHKWGCBytLtn0
         active: true
         carts
             0
                 amount: 11
                 userID: "google:116879660852602200588"
             1
                 amount: 12
                 userID: "google:116879660852602200589"
         description: ""
         details:  ""
         ean: ""
         id: "-KG8O_5iHKWGCBytLtn0"
         image: "iVBORw0KGgoAAAANSUhEUgAABqsAAAPACAIAAAAt5NQJAAA..."
         name: "test"
         price: 1
         stock: 0
         stockavailable: 0
         tax: 1
         users: ""
     -KG9-r5ATr1BokkVzuYt
         active: true
         carts
             0
                 amount: 11
                 userID: "google:116879660852602200588"
             1
                 amount: 12
                 userID: "google:116879660852602200589"
         description: ""
         details: ""
         ean: ""
         id: "-KG9-r5ATr1BokkVzuYt"
         image: "iVBORw0KGgoAAAANSUhEUgAABqsAAAPACAIAAAAt5NQJAAA..."
         name: "test"
         price: 1
         stock: 0
         stockavailable: 0
         tax: 0.25
         users: ""

【问题讨论】:

    标签: firebase firebase-realtime-database firebaseui


    【解决方案1】:

    您可以使用查询告诉 Firebase 服务器过滤返回的数据:

    Firebase ref = new Firebase("https://yours.firebaseio.com");
    Firebase products = ref.child("products");
    Query userProducts = products.orderByChild("userId").equalTo(authData.uid);
    
    FirebaseListAdapter<Product> adapter = new FirebaseListAdapter<Product>(
        this,
        Product.class,
        R.layout.product_layout,
        userProducts
    ) { ...
    

    这在Firebase documentation on queries 中有记录,我强烈建议您阅读。在Firebase guide for Android developers 上花费几个小时,将为您节省很多时间。

    针对此用例更优化的数据结构

    虽然这可行,但您似乎已将类似 SQL 的结构应用于 NoSQL 数据库。要返回 userProducts,Firebase 服务器必须考虑所有产品。随着产品*用户列表的增长,这将开始花费越来越多的时间。

    读取优化程度更高的结构考虑到您通常希望为用户访问产品,因此它为每个用户存储产品。

    UserProducts
      uid1
        product1: { ... }
        product3: { ... }
        product5: { ... }
      uid2
        product2: { ... }
        product4: { ... }
        product6: { ... }
    

    现在您可以为用户访问产品:

    Firebase ref = new Firebase("https://yours.firebaseio.com");
    Firebase products = ref.child("UserProducts");
    Firebase userProducts = products.child(authData.uid);
    
    FirebaseListAdapter<Product> adapter = new FirebaseListAdapter<Product>(
        this,
        Product.class,
        R.layout.product_layout,
        userProducts
    ) { ...
    

    【讨论】:

    • 谢谢!我考虑过使用第二个选项,但后来我不得不在多个地方更新产品更改,感觉不太好。
    • 这是你的 SQL 腺体。一旦你的 NoSQL 腺体占据主导地位,这种感觉就会变得更好。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    相关资源
    最近更新 更多