当我使用用户 account1 登录并上传数据时,可以说“你好”并获取它,我可以看到“你好”这个词(这是我想要的)。但是当我注销并使用用户 account2 登录时,每当我单击 fetch 时,它都会显示“Hello”。但它不应该显示,因为这是由用户 account1 输入的。所以它应该只显示登录用户输入的数据。
我将如何更改我的代码以只允许当前登录的用户存储和检索数据?
这是下面的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseApp.initializeApp(this);
setContentView(R.layout.activity_ichild);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference uidRef = rootRef.collection("RootCollection").document(uid);
POJO pojo = new POJO();
uidRef.set(pojo);
fetch=findViewById(R.id.fetchDocument);
firestore = FirebaseFirestore.getInstance();
upload =findViewById(R.id.uploadText);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
POJO pojo = new POJO();
pojo.setValue("Hello World");
//below code creates collection, creates a inner doc. and uploads the value (pojo)
firestore.collection("RootCollection").document("MyDoc")
.set(pojo).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//gets called if upload is successful
Toast.makeText(ichild.this,"Upload is successful", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ichild.this,"Upload is not successful" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
MYYA
相关分类