如何使用Firestore获取当前登录用户关联的文档数据?
问题分析与解决方案
崩溃直接原因
你的代码里while (document.exists())是无限死循环:document.exists()一旦为true,循环内没有任何逻辑改变这个条件,会导致APP一直循环执行,内存耗尽后崩溃。
另外还有两个潜在问题:
- 未判断用户是否登录:如果用户未登录,
FirebaseAuth.getInstance().getCurrentUser()会返回null,调用getUid()直接触发空指针异常。 - 数据查询逻辑错误:你当前是获取单个用户ID对应的文档,但根据需求应该是获取该用户关联的多个商品数据,需要根据你的Firestore结构调整查询方式。
修正方案(分两种常见结构)
结构1:每个商品是images集合下的独立文档,文档包含userID字段关联用户
// 先检查用户是否登录 FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser(); if (currentUser == null) { // 处理未登录场景,比如跳转登录页 return; } String uid = currentUser.getUid(); // 查询当前用户的所有商品 fStore.collection("images") .whereEqualTo("userID", uid) .get() .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { itemList = new ArrayList<>(); // 遍历查询到的所有商品文档 for (QueryDocumentSnapshot doc : task.getResult()) { Item item = new Item(); item.setItemName(doc.getString("name")); item.setItemPrice(doc.getString("price")); item.setItemDescription(doc.getString("description")); item.setItemImage(doc.getString("image")); itemList.add(item); } // 初始化适配器并绑定RecyclerView shoeAdapter = new ShoeAdapter(MainActivity.this, itemList); recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); // 必须设置布局管理器 recyclerView.setAdapter(shoeAdapter); } else { // 打印查询错误信息 Log.e("FirestoreError", "查询失败", task.getException()); } } });
结构2:用户ID文档下有items子集合存储商品
如果你的Firestore结构是images -> 用户ID文档 -> items子集合(每个文档是商品),则用以下查询:
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser(); if (currentUser == null) { return; } String uid = currentUser.getUid(); fStore.collection("images") .document(uid) .collection("items") .get() .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { itemList = new ArrayList<>(); for (QueryDocumentSnapshot doc : task.getResult()) { Item item = new Item(); item.setItemName(doc.getString("name")); item.setItemPrice(doc.getString("price")); item.setItemDescription(doc.getString("description")); item.setItemImage(doc.getString("image")); itemList.add(item); } shoeAdapter = new ShoeAdapter(MainActivity.this, itemList); recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); recyclerView.setAdapter(shoeAdapter); } else { Log.e("FirestoreError", "查询失败", task.getException()); } } });
优化模型类(可选)
可以给模型类添加Firestore字段映射注解,让数据解析更简洁:
package com.bac.shoesrecyclerview; import com.google.firebase.firestore.PropertyName; public class Item { @PropertyName("name") private String itemName; @PropertyName("price") private String itemPrice; @PropertyName("description") private String itemDescription; @PropertyName("image") private String itemImage; public Item() {} // Firestore必须有无参构造 public Item(String itemName, String itemPrice, String itemDescription, String itemImage) { this.itemName = itemName; this.itemPrice = itemPrice; this.itemDescription = itemDescription; this.itemImage = itemImage; } @PropertyName("name") public String getItemName() { return itemName; } @PropertyName("name") public void setItemName(String itemName) { this.itemName = itemName; } @PropertyName("price") public String getItemPrice() { return itemPrice; } @PropertyName("price") public void setItemPrice(String itemPrice) { this.itemPrice = itemPrice; } @PropertyName("description") public String getItemDescription() { return itemDescription; } @PropertyName("description") public void setItemDescription(String itemDescription) { this.itemDescription = itemDescription; } @PropertyName("image") public String getItemImage() { return itemImage; } @PropertyName("image") public void setItemImage(String itemImage) { this.itemImage = itemImage; } }
之后解析时可以直接用Item item = doc.toObject(Item.class);替代手动赋值。
内容的提问来源于stack exchange,提问作者jayCoder
相关产品推荐
相关产品推荐

