【问题标题】:How to add data in firebase for each user in a separate collection using the ID of user as the name of the collection?如何使用用户 ID 作为集合名称为单独集合中的每个用户在 firebase 中添加数据?
【发布时间】:2020-08-11 12:17:16
【问题描述】:

确切地说,如何使用用户 ID 作为集合名称为单独的集合中的每个用户添加数据,以防止数据对所有人可见?我正在一个项目中工作,用户在该项目中保存各自的数据并保存它。保存数据后,数据将保存在我创建的集合中的 firebase 中。我的问题是,保存的数据存储在集合中,所有其他用户(包括不同用户)保存的整个数据都显示给每个用户。我正在寻找显示特定用户保存的数据(在我的情况下为 recyclerview)而不是显示存储在集合中的所有其他数据的东西。

我相信这是通过为每个用户创建一个单独的集合来实现的,我不知道这些的实现

我希望我可以共享许多 java 文件,因为我不想让它变得混乱和冗长,我只是发布一个从 firebase 检索数据的片段类。如果您需要任何其他文件,我很乐意分享。感谢阅读!

package com.cksapp.memoryin;

package com.cksapp.memoryin;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;


/**
 * A simple {@link Fragment} subclass.
 */
public class Third extends Fragment {
    View v;


    private RecyclerView mRecyclerview;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    CollectionReference noteref = db.collection("Notebook");
    private Adapterrrr adapterrrr;

    public Third() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        v = inflater.inflate(R.layout.fragment_third, container, false);
        mRecyclerview = (RecyclerView) v.findViewById(R.id.recycler_view_interest);

        setuprecyclerview();
        return v;
    }

    private void setuprecyclerview() {
        Query query = db.collection("Notebook");
        FirestoreRecyclerOptions<CardModel> options = new FirestoreRecyclerOptions.Builder<CardModel>()
                .setQuery(query, CardModel.class)
                .build();


        adapterrrr = new Adapterrrr(options);
        mRecyclerview = (RecyclerView) v.findViewById(R.id.recycler_view_interest);
        //mRecyclerview.setHasFixedSize(true);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerview.setAdapter(adapterrrr);
    }

    @Override
    public void onStart() {
        super.onStart();
       adapterrrr.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapterrrr.stopListening();
    }
}

我现在有 3 个用户,这三个用户都可以访问存储在 firebase 中的数据。所以从技术上讲,每个在我的应用程序中注册的用户都会添加和访问数据。我想让它专门为每个保存的用户保存和检索数据。

因此,在我的情况下,所有用户都可以访问 recyclerview 中的数据,无论谁可能添加了数据,这些数据对所有用户都是通用的。如何使其成为特定于用户的数据?

【问题讨论】:

  • 我不是很理解这里的问题。如果您现有的模型不起作用,您的问题应该具体说明什么没有按您期望的方式起作用。

标签: android firebase google-cloud-firestore


【解决方案1】:

最常见的方法是创建一个名为 owner 的字段,该字段将在每个文档中具有 User UID 的值:

/Notebooks/{notebook_id}

{
  name: "a",
  amount: "2",
  owner: "user_uid"
}

并将您的查询更改为:

var user = firebase.auth().currentUser;
Query query = db.collection("Notebook").where("owner", "==", user.uid).get();

另外建议添加安全规则以避免未经身份验证的用户读写:

service cloud.firestore {
  match /databases/{database}/documents {
    match /Notebooks/{notebook_id} {
      allow read, write: if request.auth != null && request.auth.uid == resource.data.owner;
    }
  }
}

您可以在此处找到有关如何通过特定用户 ID 保护文档的更多信息:Secure and query documents based on auth.uid

【讨论】:

    【解决方案2】:

    感谢您抽出时间阅读我的帖子。我能够找出解决方案。我已更改有关身份验证用户 ID 的查询并为用户 ID 生成子集合类。

    所以,层次结构如下

    1. 主要收藏。 笔记本
    2. 用户ID 用户ID
    3. userHourbook(新系列)

    还有我的查询代码的变化 只是层次的表示

    Query query = db.collection("Hourbook").document(user.getUid()).collection("userHourbook");
    

    【讨论】:

      猜你喜欢
      • 2022-07-13
      • 2022-01-13
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2022-06-11
      相关资源
      最近更新 更多