【发布时间】:2017-12-18 13:55:40
【问题描述】:
我想在我的 firebase 数据库中的“服装”参考下获取所有 firebase 节点。为此,我将ChildEventListener 附加到引用,并在onChildAdded 回调中,我将Clothing 对象添加到服装对象列表中,假设onChildAdded 回调被调用的次数是下面有节点的次数“服装”参考。
mClothingRef = FirebaseDatabase.getInstance()
.getReference()
.child("clothing");
final List<Clothing> clothingItems = new ArrayList<>();
mClothingRef.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot snapshot, String s) {
Clothing clothing = snapshot.getValue(Clothing.class);
clothingItems.add(clothing);
Log.d(TAG, "onChildAdded called");
}
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.getMessage() + " " +
databaseError.getCode() + " " + databaseError.getDetails() + " " + databaseError.toString());
mEventBus.post(new ListClothingFailEvent());
}
...
}
这是数据库结构:
-->root
---->clothing
------>clothing_id
-------->title
-------->category
-------->img_download_url
------>clothing_id_1
-------->title
-------->...
我需要获取服装节点下的所有节点。
我的数据库安全规则目前是:
{
"rules": {
".read": "auth == null",
".write": "auth != null"
}
}
当调用包含此代码的方法时,onChildAdded 回调不是,而是onCancelled 回调带有权限被拒绝的数据库错误。为什么会这样?
【问题讨论】:
-
当我在该行上添加一个断点时,它将被忽略,因为该行从未被调用。根据调试器,服装节点的参考 url 是:good-deal-9a0b2.firebaseio.com/clothing,这似乎是正确的
-
@TomFinet 你能分享你完整的活动代码吗?
-
此代码不在活动中,但使用
mCatalogRepository.loadClothing()从活动中调用。我会把它添加到问题中。
标签: android firebase firebase-realtime-database