等待 Cloud Firestore 任务结果

huangapple go评论103阅读模式
英文:

Wait for Cloud Firestore task result

问题

我正试图开发一种方法来访问 Cloud Firestore,读取用户数据并返回与之关联的映射。据我理解,它没有等待成功/失败监听器,我已经尝试将其更改为完全监听器,但结果是相同的。它跳过了这些监听器。
我知道这是一个异步过程,但我需要接收数据以在调用该方法的 Activity 中显示它们。

Settings Activity:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. this.binding = ActivitySettingsBinding.inflate(getLayoutInflater());
  5. this.fbUtils = new FirebaseHandler(this);
  6. setContentView(binding.getRoot());
  7. }
  8. @Override
  9. protected void onStart() {
  10. super.onStart();
  11. User user = fbUtils.getCurrentUser();
  12. if (user == null) {
  13. findUserOrLogout();
  14. } else {
  15. showUserData(user);
  16. }
  17. }

FirebaseHandler:

  1. public User getCurrentUser() {
  2. FirebaseUser user = auth.getCurrentUser();
  3. if (user != null) {
  4. this.userMap = getUserDBInfo(user.getUid());
  5. this.profilePic = getUserPic(user.getUid());
  6. if (this.userMap != null && this.profilePic != null) {
  7. return new User(user, userMap, profilePic);
  8. }
  9. }
  10. return null;
  11. }
  12. private Map<String, Object> getUserDBInfo(String uid) {
  13. Log.d(TAG, "Getting user info for user '" + uid + "'");
  14. DocumentReference docRef = db.collection("users").document(uid);
  15. final Map<String, Object>[] userMap = new Map[]{null};
  16. final boolean[] done = new boolean[]{false};
  17. docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  18. @Override
  19. public void onSuccess(DocumentSnapshot documentSnapshot) {
  20. if (documentSnapshot.exists()) {
  21. Log.d(TAG, "DocShot: " + documentSnapshot);
  22. userMap[0] = documentSnapshot.getData();
  23. } else {
  24. Log.d(TAG, "User's document not found");
  25. }
  26. }
  27. }).addOnFailureListener(new OnFailureListener() {
  28. @Override
  29. public void onFailure(@NonNull Exception e) {
  30. Log.d(TAG, "Couldn't get the Document");
  31. MainActivity.showToast("Couldn't get the Document", activity.getApplicationContext());
  32. }
  33. });
  34. return userMap[0];
  35. }

我在代码中做错了什么,还是有什么可以改变的地方使其工作起来?
提前感谢。

英文:

I'm trying to develop a method to access the Cloud Firestore, read a user's data and return the map associated to it. For what I understand, it is not waiting for the success/failure listener, and I already tried changing it for a complete listener but the result is the same. It skips through the listeners.
I know that this is a Assynchronous process, but I need to receive the data to show them on the Activity that calls the method.

Settings Activity:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. this.binding = ActivitySettingsBinding.inflate(getLayoutInflater());
  5. this.fbUtils = new FirebaseHandler(this);
  6. setContentView(binding.getRoot());
  7. }
  8. @Override
  9. protected void onStart() {
  10. super.onStart();
  11. User user = fbUtils.getCurrentUser();
  12. if (user == null) {
  13. findUserOrLogout();
  14. } else {
  15. showUserData(user);
  16. }
  17. }

FirebaseHandler

  1. public User getCurrentUser() {
  2. FirebaseUser user = auth.getCurrentUser();
  3. if (user != null) {
  4. this.userMap = getUserDBInfo(user.getUid());
  5. this.profilePic = getUserPic(user.getUid());
  6. if (this.userMap != null &amp;&amp; this.profilePic != null) {
  7. return new User(user, userMap, profilePic);
  8. }
  9. }
  10. return null;
  11. }
  12. private Map&lt;String, Object&gt; getUserDBInfo(String uid) {
  13. Log.d(TAG, &quot;Getting user info for user &#39;&quot; + uid+&quot;&#39;&quot;);
  14. DocumentReference docRef = db.collection(&quot;users&quot;).document(uid);
  15. final Map&lt;String, Object&gt;[] userMap = new Map[]{null};
  16. final boolean[] done = new boolean[]{false};
  17. docRef.get().addOnSuccessListener(new OnSuccessListener&lt;DocumentSnapshot&gt;() {
  18. @Override
  19. public void onSuccess(DocumentSnapshot documentSnapshot) {
  20. if (documentSnapshot.exists()) {
  21. Log.d(TAG, &quot;DocShot: &quot; + documentSnapshot);
  22. userMap[0] = documentSnapshot.getData();
  23. } else {
  24. Log.d(TAG, &quot;User&#39;s document not found&quot;);
  25. }
  26. }
  27. }).addOnFailureListener(new OnFailureListener() {
  28. @Override
  29. public void onFailure(@NonNull Exception e) {
  30. Log.d(TAG, &quot;Couldn&#39;t get the Document&quot;);
  31. MainActivity.showToast(&quot;Couldn&#39;t get the Document&quot;, activity.getApplicationContext());
  32. }
  33. });
  34. return userMap[0];
  35. }

Am I doing something wrong in my code, or is there anything I can change to make this work?

Thanks in advance.

答案1

得分: 1

你必须在Firebase控制台内更改规则,以允许写入操作。

您需要按照以下步骤进行操作:

  1. 打开Firebase控制台,进入Database(数据库)。

  2. 在那里转到**Rules(规则)**选项卡,并编辑规则如下:

  1. {
  2. "rules": {
  3. ".read": true,
  4. ".write": true
  5. }
  6. }

这将允许您在数据库上执行写入操作。

如果您需要截屏,请告诉我。

英文:

You have to change the rules inside the Firebase console to allow a write operation.

You've to follow the steps given below to do so:-

  1. Open your Firebase Console and navigate to Database.

  2. There go to the Rules tab and edit the rules to following:-

  1. {
  2. &quot;rules&quot;: {
  3. &quot;.read&quot;: true,
  4. &quot;.write&quot;: true
  5. }
  6. }

This will allow you to perform write operations on your DB.

Please let me know if you will need screenshots.

答案2

得分: 0

如果您的数据库需要经过身份验证的用户才能读写数据库,请不要使用此处的其他答案。这将创建一个巨大的安全漏洞,允许任何拥有互联网连接的人完全读写数据库。

相反,您的代码应该在尝试进行任何查询之前确保用户已登录。这可以通过使用身份验证状态侦听器来实现,以便在用户登录时获得回调。只有在使用有效的用户对象调用此回调后,才能安全地进行需要身份验证的查询。

英文:

If your database requires an authenticated user to read and write the database then do use the other answer here. It will create a massive security hole that allows for anyone with an internet connection to fully read and write the database.

Your code should instead ensure that a user is signed in before trying to make any queries. This made possible by using an auth state listener to get a callback when the user is signed in. Only after this callback is invoked with a valid user object is it safe to make a query that requires authentication.

huangapple
  • 本文由 发表于 2020年8月15日 23:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63427953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定