Search code examples
androidfirebasefirebase-realtime-databasedata-access-layercoding-style

Firebase Back-end code arrangement in Android Studio


I am working on a college project, my team and I are developing an application that includes users and groups, we are using Firebase for signing-up using Facebook, and almost every activity needs to get data from the Firebase real-time database. I am wondering if there is a coding convention for writing the code of manipulating the database. For example, a lot of code samples look like this :

createGroupBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        group.addUser(firebaseuser.getUid());
            //add group to firebase
            String groupId=FirebaseDatabase.getInstance().getReference().child("Groups").addGroup(group);

            //set user's group id for this id
            FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseuser.getUid()).child("groupId").setValue(groupId);

I think it's so messy and not the right way of accessing the Database (inside of an activity.java class file). I can't think about a good way of arranging the code and creating data access layer that will make the code better and make my developing process go easier. Any suggestions will help.


Solution

  • Wrap the Firebase calls into a service layer and expose the service methods.