如何生成 firebase uid
How to generate firebase uid
我正在尝试将数据(不是用户)存储在 Firebase 后端。如何生成 Firebase 唯一标识符并将其存储在 child 中?我一直在使用 firebase(DatabaseReference 键)作为记录的唯一标识符,但键为空。
您可以使用push().getKey()
获取唯一键
FirebaseDatabase database = FirebaseDatabase.getInstance();
String key = database.getReference("todoList").push().getKey();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put( key, todo.toFirebaseObject());
database.getReference("todoList").updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
}
}
});
这在 更新特定字段 部分 docs 部分
中进行了解释
This example uses push() to create a post in the node containing posts
for all users at /posts/$postid and simultaneously retrieve the key
with getKey(). The key can then be used to create a second entry in
the user's posts at /user-posts/$userid/$postid.
FirebaseDatabase database = FirebaseDatabase.getInstance();
String key = database.getReference("todoList").push().getKey();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put( key, todo.toFirebaseObject());
database.getReference("todoList").updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
}
}
});
我正在尝试将数据(不是用户)存储在 Firebase 后端。如何生成 Firebase 唯一标识符并将其存储在 child 中?我一直在使用 firebase(DatabaseReference 键)作为记录的唯一标识符,但键为空。
您可以使用push().getKey()
获取唯一键
FirebaseDatabase database = FirebaseDatabase.getInstance();
String key = database.getReference("todoList").push().getKey();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put( key, todo.toFirebaseObject());
database.getReference("todoList").updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
}
}
});
这在 更新特定字段 部分 docs 部分
中进行了解释This example uses push() to create a post in the node containing posts for all users at /posts/$postid and simultaneously retrieve the key with getKey(). The key can then be used to create a second entry in the user's posts at /user-posts/$userid/$postid.
FirebaseDatabase database = FirebaseDatabase.getInstance();
String key = database.getReference("todoList").push().getKey();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put( key, todo.toFirebaseObject());
database.getReference("todoList").updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
}
}
});