当我使用用户 account1 登录并上传数据时,可以说“你好”并获取它,我可以看到“你好”这个词(这是我想要的)。但是当我注销并使用用户 account2 登录时,每当我单击 fetch 时,它都会显示“Hello”。但它不应该显示,因为这是由用户 account1 输入的。所以它应该只显示登录用户输入的数据。我将如何更改我的代码以只允许当前登录的用户存储和检索数据?这是下面的代码:@Overrideprotected 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(); } }); } });
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
因为每次您将数据加载到同一个文档时
firestore.collection("RootCollection").document("MyDoc")
相反,您可以使用在路径中引用 id 的文档,所以使用这个
uidRef.set(pojo).addOnSuccessListener(new OnSuccessListener<Void>() {
代替
firestore.collection("RootCollection").document("MyDoc")
.set(pojo).addOnSuccessListener(new OnSuccessListener<Void>() {
并删除它,只需设置空对象
POJO pojo = new POJO();
uidRef.set(pojo);
添加回答
举报
0/150
提交
取消