如何生成firebase uid [英] How to generate firebase uid

查看:63
本文介绍了如何生成firebase uid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据(而非用户)存储在Firebase后端中.如何生成Firebase唯一标识并将其存储在子级中?我一直在使用firebase(DatabaseReference Key)作为记录螺母的唯一标识符,该键为null.

I am trying to store data (not users) in a Firebase backend. How do I generate Firebase unique identifer and store it in the child? I have been using firebase (DatabaseReference Key) as the unique identifier of a record nut the key is null.

推荐答案

您可以使用push().getKey()获取唯一密钥

You can use push().getKey() to get the unique key

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 is explained in the docs under Update specific fields section

此示例使用push()在包含帖子的节点中创建帖子 为所有用户在/posts/$ postid并同时检索密钥 使用getKey().然后可以使用该密钥在其中创建第二个条目 用户的帖子位于/user-posts/$ userid/$ postid.

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.

这篇关于如何生成firebase uid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆