Abhinav Srivastava
Abhinav Srivastava

Reputation: 185

Accessing data from firebase realtime database inside recyclerview

My recyclerview gets data from json api and populate list of articles. I have now implemented a commenting system with the help of firebase realtime database. I want to show number of comments below each article image in recyclerview. I tried several methods to implement that but all of them are not very effective.

  1. At first I implemented database query based on article unique id for each view but since recyclerview has over 100 articles so it was making over 100 instance calls to database and was causing huge bandwidth problem.

  2. Then I made one query to get all comments count from database and saved them locally in SQLite database and inside recyclerview i query SQLite databse to get comments counts but inserting 100 rows with article id and comments count in SQLite is slow.

What do you guys recommend best method for such task where I will spend least amount of bandwidth and get comment counts also?

My db structure is like this.

enter image description here

get comments method

public void getComments() {
    keyrf = FirebaseDatabase.getInstance().getReference().child("Keys");
    keyrf.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            HashMap map = new HashMap();
            for( DataSnapshot child : dataSnapshot.getChildren() ) {
                String childKey = child.getKey();
                String c = child.child("c").getValue().toString();

                map.put(childKey, c);
                addComments(MainActivity.this, childKey, c);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

insert comments method

public static void addComments(Context context, String childKey, String c) {
    try {
        SQLiteDatabase myDB = context.openOrCreateDatabase("MyDb", Context.MODE_PRIVATE, null);

        myDB.execSQL("CREATE TABLE IF NOT EXISTS comments (articleId INTEGER not null unique, comment INTEGER not null)");
        String sql = "REPLACE INTO comments (articleId, comment) VALUES (?, ?)";
        SQLiteStatement statement = myDB.compileStatement(sql);
        statement.bindString(1, childKey);
        statement.bindString(2, c);
        statement.execute();
    } catch (Exception e) {
    }
}

Upvotes: 3

Views: 127

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138804

What do you guys recommend best method for such task where I will spend least amount of bandwidth and get comment counts also?

The workaround here is to keep a count property somewhere in the database and update that whenever you add/delete child nodes.

So you can consider using a new section that might look similar to this:

Fireabase-root
   |
   --- numberOfComments
          |
          --- commentId: 12
          |
          --- commentId: 10
          |
          --- commentId: 20

So everytime you add or delete a post, increase / decrease that count by one. And because the number of comments might be updated in an multi user environment, I recommend you to use FirebaseTransactions as explained my answer from this post.

Upvotes: 1

Related Questions