使用Firebase Android文档保存数据列表:
String new_status;
new_status = status_update_text.getText().toString();
Firebase postRef = userRef.child("posts");
Map<String, String> post1 = new HashMap<String, String>();
post1.put("status", new_status);
postRef.push().setValue(post1);
用户身份验证后用户保存到数据库:
//save user to database
Map<String, String> map = new HashMap<String, String>();
map.put("provider", authData.getProvider());
if(authData.getProviderData().containsKey("displayName")) {
map.put("displayName", authData.getProviderData().get("displayName").toString());
}
ref.child("users").child(authData.getUid()).setValue(map);
目前的安全规则:
"rules": {
".read": true,
"$posts": {
// create new post if it doesn't exist but can't modify or delete posts
".write": "!data.exists() || newData.exists()"
}
}
问题是当用户再次登录时,所有已保存的数据都被.setValue(map)删除(我假设)。我试图把写规则尽可能高,因为我已经读过规则级联但我不明白为什么它当前被忽略了???
答案 0 :(得分:0)
你的情况错了:
!data.exists() || newData.exists()
假设您将数据写入已包含数据的位置。评估结果为:
!true || true => true
您可能正在寻找:
!data.exists()
答案 1 :(得分:0)
如果我正确地阅读了问题,问题不在于您的规则,而在于每次用户验证以前保存的信息时都会被删除。如果是这种情况,那么我会说你的问题正如你猜测的那样,
ref.child("users").child(authData.getUid()).setValue(map);
setValue()函数将转到您的特定用户节点并清除所有其他数据 - 仅将其替换为displayName。
我认为你希望updateChildren()方法与setValue()
相对ref.child("users").child(authData.getUid()).updateChildren(map);
这应该只更新用户名并保留其余数据。