1

In my navigation drawer layout, I show the user's login in a text view. The later can be changed if he clicks on a button titled "Edit profile" and then submits the corresponding form. That's why I use a snapshot listener, which updates the text view.

Since the navigation drawer layout exists at any execution time, when should I detach the listener?

I could detach it if the user clicks on the button "Log out", but what if he never clicks on it?

JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70
  • 1
    Doesn't matter what views you are you using or when you login or logout, you should detach the listener according to the life-cycle of your activity. Logins are persistent and will be retained until you call the signOut API, so please check the duplicate to see how you can achieve this. – Alex Mamo Apr 26 '19 at 08:14

1 Answers1

1

The better practice will be save login in sharedPref first time and get it all the time for setting in the textView when needed. So in this case you don't need to make requests all the time drawer open. The second advantage will be it will work much faster. Third advantage is you will reduce the number of requests instead of making requests all the time you will just make request to update if needed so instead of thousands several same reading requests you will make one update request and everything gonna happen locally. And even if user will not have internet connection textView will show needed information. And you will not pay money in case you get a lot of users for latency to google cloud. Also it answer the question of what to do with listener

Bo Z
  • 2,359
  • 1
  • 13
  • 31
  • Thanks! In the case I couldn't use shared preferences but only NoSQL queries using a Firestore snapshot listener: How would you deal with my problem? – JarsOfJam-Scheduler Apr 25 '19 at 21:02
  • @JarsOfJam-Scheduler that what I am saying. Please read carefully my answer. You need to avoid make queris all the time for this textView. You will pay money for that to Google cloud. You need to change the logic of the code and instead of making all that queries you will just save it ONCE in Firebase and SharedPref at the same time. And for textView instead of getting name from Firebase you will get it from LOCAL storage SharedPref. Try to count how many queries you suppose to do for 3-4-5 fields - millions. And you will pay for that. You need to change logic – Bo Z Apr 25 '19 at 21:37
  • I correctly understood your answer. But querying the database could be required in some situations (other than the context I've described), that's why I asked for that. – JarsOfJam-Scheduler Apr 26 '19 at 16:51
  • Did my answer was useful? If so could you mark it as an answer? – Bo Z Jul 02 '19 at 13:10
  • Hi Boris:)! Thanks to up my intention :). It's marked as an answer now. Thank you again my friend' – JarsOfJam-Scheduler Jul 02 '19 at 13:58
  • @JarsOfJam-Scheduler you are welcome. Your answer also teached me a lot. Good luck – Bo Z Jul 02 '19 at 13:58