gpt4 book ai didi

android - 如何正确嵌套数据并写入Firebase

转载 作者:行者123 更新时间:2023-11-30 00:54:43 25 4
gpt4 key购买 nike

我有一些允许用户输入数据的 Edittext:

AppName
Date
Title
Quantity
User

我开始了一个新的 Firebase 项目,它目前是一片空白。

我正在这样设置我的 Firebase 实例:

private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://myapp.firebase.io");

我想要的是每次出现新的添加时,我想像这样添加它(AppName 是所有条目的共同点):

AppName
Date
Title
Quantity
User

Date
Title
Quantity
User
...
...

AppName
Date
Title
Quantity
User

Date
Title
Quantity
User
...
...

我怎样才能做到以下几点:

The first time it is added, the AppName will be the parent node, and anytime after Firebase should check to see if the parent node exist and is the same and add it to that, otherwise create a new parent node and add the children node to the new parent node.

我目前的身份验证设置如下:

{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}

我必须将我的应用更改为 true 才能写入 Firebase。

我怎样才能:

Not register the user but only allow the app to able to write to Firebase.

编辑:

我正在做以下事情:

mDatabase.child("AppName").setValue(etName.getText().toString()); 
mDatabase.child("AppName").child("Date").setValue(etDate.getText().toString());
mDatabase.child("AppName").child("Title").setValue(etQuantity.getText().toString());
mDatabase.child("AppName").child("Quantity").setValue(User.getText().toString());
mDatabase.child("AppName").child("User").setValue(etUser.getText().toString());

它不是添加,而是覆盖以前的条目。我该如何追加。

最佳答案

如果我理解你的话,你需要非注册用户可以在 Firebase 上编辑你的数据库,以及如何知道你是否添加了 ID。

首先,正如Ishan Fernando所说,如果你添加相同id的数据,这个数据会被覆盖,使用push方法,Firebase会创建一个unic ID来添加你的数据

阅读:https://firebase.google.com/docs/database/android/read-and-write?hl=en

这个 Gist,感谢用户 realgt,可以帮助您知道 Appname 是否存在:https://gist.github.com/anantn/4323949#gistcomment-998628这将获取您的所有数据,您可以检查是否需要添加它

而且,如果你不想注册用户,但你需要他们来写数据库,我认为你需要将他们注册为匿名用户: https://firebase.google.com/docs/auth/android/anonymous-auth

Firebase 需要注册用户(使用社交网络、电子邮件或匿名)来修改它,所以试试匿名方法。问题是当用户离开您的应用程序时,该用户将继续在您的数据库中注册。因此,我建议您(如果我错了请纠正我)在 onStop 启动时删除用户并在 onReady 启动时重新创建它。

希望对你有帮助。

关于android - 如何正确嵌套数据并写入Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40378165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com