androidfirebasefirebase-realtime-databaseuser-tracking

Firebase database structure - storing user activity in order to customize views and permission


Working with Firebase for the first time and looking for advice of setting up the right structure for my project which is basically an "offers/coupon" type starter project.

The scenario is this:

  1. I have a node containing a list of all offers available to users

  2. This list of offers is displayed to users after successful Firebase authentication

  3. When a user redeems an offer, I want to be able to count/record that activity in their child node under user and hide that offer so that they cannot see it again once used.

My question is what would be the best way to do this given that offers may be added, may expire, or may change at some point in the future. So, in effect, the user should receive the list of most updated offers, minus the ones he/she have used in the past.

a) would it be more effective to have a master list of offers, and then run a cloud/server function to clone this list for each new user an track that way

Firebase Structure 1

or

b) Keep a master list of offers in one node, then track user specific offer usage

Firebase Structure 2

Appreciate your guidance


Solution

  • The second solution is better because you'll save bandwith. This practice is called denormalization and is a common practice when it comes to Firebase. The first solution is not good becase every time you want to display the users you donwload unnecessary data. If you want to read more details about how you can structure a Firebase database in a efficient way, please read this post, Structuring your Firebase Data correctly for a Complex App. Also, you can take a look a this tutorial, Denormalization is normal with the Firebase Database, for a better understanding.