Java 将新数据推送到 Firebase 数据库时设置自定义键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37483406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting custom key when pushing new data to firebase database
提问by Victor Davis Lenz
Well, I am new to Firebase and I want to have my own keys while pushing new data to database.
好吧,我是 Firebase 的新手,我想在将新数据推送到数据库时拥有自己的密钥。
Problem:
问题:
FireBase.push().setValue(mapped_values);
This gives structure like below:
这给出了如下结构:
How can I create my own custom key there? Such as username or something.
如何在那里创建自己的自定义密钥?比如用户名什么的。
回答by Frank van Puffelen
Calling push()
will generate a key for you.
呼叫push()
将为您生成密钥。
If instead you use child()
, you can determine they key/path yourself.
如果您使用child()
,则可以自己确定它们的键/路径。
ref.child("Victor").setValue("setting custom key when pushing new data to firebase database");
回答by Rand
You can create a custom key using setValue()even if the root contains many children for example if 'Users' is the root and you want to add users with email as a key it will be like this
您可以使用setValue()创建自定义键,即使根包含许多子项,例如,如果“用户”是根并且您想添加电子邮件作为键的用户,它将是这样的
firebase.child("firebase url").child("Users").child("user_1 email").setValue(...)
firebase.child("firebase url").child("Users").child("user_2 email").setValue(...)
etc
等等
Hope this helps.
希望这可以帮助。
回答by iqbal lone
String key="1234567sdfsf8";
//custom object
User user=new User();
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Users").child(key).setValue(user);
回答by Phil
If you are using FirebaseUI:
如果您使用FirebaseUI:
private static final CollectionReference usersCollection = FirebaseFirestore.getInstance().collection("users");
User user = new User("MyUsername", "MyPictureUrl");
String userKey = "1234567sdfsf8";
usersCollection.document(userKey).set(user); //MAGIC LINE
回答by Talha
Just for sharing the knowledge.
只是为了分享知识。
if you are using fire-sharp, you can create the custom key as follows
如果您使用的是 fire-sharp,则可以按如下方式创建自定义密钥
IFirebaseConfig config = new FirebaseConfig
{
AuthSecret = "SecretKey",
BasePath = "https://abc.firebaseio.com/",
Host = "abc.firebaseio.com/"
};
IFirebaseClient client = new FirebaseClient(config);
var obj = new Users
{
FirstName = "test",
MiddleName = "user",
LastName = "xyz"
};
SetResponse response = client.SetAsync("Profile", "YourID");//you can use Set() as well
response = client.SetAsync("Profile/YourID", obj);//you can use Set() as well
回答by Tarsbir Singh
Simple and Fast
简单快速
Map<String,Object> taskMap = new HashMap<>();
taskMap.put("Name", name.getText().toString());
taskMap.put("km", km.getText().toString());
// taskMap.put("age", "45");
taskMap.put("Day", day.getText().toString());
mDatabaseReference.push().setValue(taskMap);
回答by Alberto
As an update to the top answer, the Firebase API has been changedand setValue()
does not work anymore. Now you must use the set()
function instead:
作为对最佳答案的更新,Firebase API 已更改并且setValue()
不再起作用。现在您必须改用该set()
函数:
ref.child("Victor").set("setting custom key when pushing new data to firebase database");
回答by Arafat
if database reference child is fixed string, new value will not add. just it will update the previous value. for example :
如果数据库引用子项是固定字符串,则不会添加新值。只是它会更新以前的值。例如 :
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
String mText = // get data from editText or set your own custom data
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
String mText = // get data from editText or set your own custom data
now if I insert data like this:
现在,如果我插入这样的数据:
myRef.child("abc").child("cba").setValue(mText);
myRef.child("abc").child("cba").setValue(mText);
every time I insert data it will update my previous data. It will not add new data. Because my reference is fixed here(myRef.child("abc").child("cba")
// this one, which is always fixed).
每次我插入数据时,它都会更新我以前的数据。它不会添加新数据。因为我的引用在这里是固定的(myRef.child("abc").child("cba")
//这个,总是固定的)。
Now change the the value of child "cba" to a random or dynamic value which will not fix. For example:
现在将子“cba”的值更改为无法修复的随机或动态值。例如:
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
myRef.child("abc").child(String.valueOf(n)).setValue(mText);
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
myRef.child("abc").child(String.valueOf(n)).setValue(mText);
In this case it will add a new value instead of updating. because this time reference is not fixed here. it is dynamic. push() method exactly do the same thing. it generates random key to maintain unique reference.
在这种情况下,它将添加一个新值而不是更新。因为这个时间参考在这里不是固定的。它是动态的。push() 方法完全相同。它生成随机密钥以保持唯一引用。
回答by pramod singh
In POST request it will generate ID's but in PATCH, PUT request it will mention the key which will be provided by you.
在 POST 请求中,它将生成 ID,但在 PATCH、PUT 请求中,它将提及您提供的密钥。