Java – Android: App Visit Count using Firebase Database

Android: App Visit Count using Firebase Database… here is a solution to the problem.

Android: App Visit Count using Firebase Database

I want to count app visits when any user opens the app. I HAVE COMPLETED THE FOLLOWING CODE, WHICH ONLY RUNS SUCCESSFULLY WHEN INTERNET IS ON

I use FirebaseDatabase.getInstance().setPersistenceEnabled(true); Offline storage of data, also using myRef.keepSynced(true); Stay in sync.

The code is as follows:

    // COUNTER PART
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("counter");
    myRef.keepSynced(true);
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Log.i("FB", "Snapshot: " + dataSnapshot);
            long val = (long) dataSnapshot.getValue() + 1;
            myRef.setValue(val);
            mainBinding.contentLayout.textViewCounterVisits.setVisibility(View.VISIBLE);
            mainBinding.contentLayout.textViewCounterVisits.setText(getString(R.string.total_visits, val));
        }

@Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.e("FB", "Error: " + error);
        }
    });

Question:

When I open the app offline multiple times, it counts offline. When I turn on the internet, it’s just synced with the updated data, but I want to update the count by adding a new count.

For example:

  • Count is 105

  • App 1 – Opening for 10 times (offline)

  • App 2 – Opening for 15 times (online)

  • Now count is 120

  • App 1 goes online and opening app and count is updating 121.

I think it should be 131 if you count them all

Solution

I think it should be 131 if we count all.

If the mechanism is real as you described, no, it shouldn’t even be 121, it should be 116, which is normal behavior because you’re using the same counter for both applications. Here is the mechanism:

  • Count is 105
  • App 1 – Opening for 10 times (offline) -> Count is still 105
  • App 2 – Opening for 15 times (online) -> Count is 120 (105 + 15)

When App 1 is offline, it only knows the initial count of 105 and those 10 increments queued. When App 1 goes live, it takes 115 and overwrites the 120 values that exist in the database at that time. So now, the counter in the database has a value of 115. When you open App 1 again, the counter increases to 116.

This “tactic” behavior occurs because App 1 doesn’t know what App 2 is doing when offline, and vice versa. In general, when it comes to incrementing counters in a multi-user environment, this is not how you should proceed. In this case, to get consistent data, you should use transactions, as I explained in the answer to the following post:

Note, however, that Firebase transactions are not supported for offline use. Transactions cannot be cached or saved for later use. This is because transactions absolutely require round-trip communication with the server to ensure that the code within the transaction completes successfully.

So in my opinion, you should increase the count field only if the user is online and using trading operations.

P.S. If you consider trying to use Cloud Firestore at some point in time, there you can find a very useful incremental action method called FieldValue.increment() .

Related Problems and Solutions