Android Firebase如何在特定用户下保存和检索数据 [英] Android Firebase how to save and retrieve data under specific user

查看:69
本文介绍了Android Firebase如何在特定用户下保存和检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的应用程序与Firebase集成在一起,以将简单的数据保存到云中.

i am trying to integrate my app with firebase to save simple data on cloud.

示例:

用户打开应用并登录.

用户写一些东西,数据保存在云上.

user write some stuff,the data saved on cloud.

当用户再次使用该应用程序时,他会看到他的数据.

when the user will use the app again he will see his data.

我已经阅读了文档,但是我找不到任何示例结构在用户和数据之间起作用.用户登录,现在如何为该用户保存字符串/对象?

i have read the docs but i coud not find any example how the structure works between the user and the data. user logged in , now how to save strings/object for that user?

我尝试过的事情:

用户登录或验证用户身份

user login or authenticate the user

Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.addAuthStateListener(new Firebase.AuthStateListener() {
    @Override
    public void onAuthStateChanged(AuthData authData) {
        if (authData != null) {
            // user is logged in
        } else {
            // user is not logged in
        }
    }
});

现在如何在该用户对象/字符串下保存?

推荐答案

您似乎正在使用旧的Firebase Android API.使用最新的Android API,您的代码中就不再需要"https://< YOUR-FIREBASE-APP> .firebaseio.com" .我建议使用该迁移指南进行迁移.

It looks like you're using and old Android API for Firebase. With the latest Android API there is no more need for "https://<YOUR-FIREBASE-APP>.firebaseio.com" in your code. I would suggest to migrate, using that migration guide.

在您遵循指南

然后,您可以存储用户信息,例如文档:

Then you can store user information like in that documentation :

String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabase.child("users").child(userId).child("name").setValue("John");

不要忘记在Firebase中更改您的安全规则,以确保只有该用户可以访问自己的数据:

Don't forget to change your security rule in Firebase to make sure only this user can access his own data: guide here. It's very easy and quick to do and very important for keeping your user's data private.

这篇关于Android Firebase如何在特定用户下保存和检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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