0

If I set scoresRef.keepSynced(false) and use Disk Persistence, FirebaseDatabase.getInstance().setPersistenceEnabled(true); to store the data locally, will it lower down the number of "Simultaneous connections" to firebase DB as there will be no active listeners(or it isn't?) ? what may be the consequences?

Codes:

I have a custom adapter "firebaseadapter" and a class "firebasestore" with getter/setter methods. Since "calls to setPersistenceEnabled must be made before any other usage of firebase Database instance", I have made a different class extending Application(or using it in main activity class with static {} is better?).

Utility.calculateNoOfColumns is calculating the number grids to be shown based on screen size.

Moreover, Will the data get updated in client side in real time if I make any changes in firebase DB if the set scoresRef.keepSynced(false)?

public class ThreeFragment extends Fragment {
View viewThree;
ArrayList<firebasestore> list;
DatabaseReference mdatabase;
GridLayoutManager gridLayoutManager;
private  firebaseAdapter firebaseAdapter1;
FirebaseDatabase database;

public ThreeFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FirebaseApp.initializeApp(getContext());
    database= FirebaseDatabase.getInstance();
    mdatabase=database.getReference().child("DBName");
    mdatabase.keepSynced(false);

    list = new ArrayList<>();
    loadStoreDetails();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    viewThree = inflater.inflate(R.layout.fragment_three, container, false);
    int mNoOfColumns = Utility.calculateNoOfColumns(getContext());

    RecyclerView firebaseRecyclerView = (RecyclerView) 
    viewThree.findViewById(R.id.recyclerview_threeFragment1);
    firebaseRecyclerView.setHasFixedSize(true);
    firebaseAdapter1 = new firebaseAdapter(getContext(), list);
    firebaseRecyclerView.setLayoutManager(gridLayoutManager);
    firebaseRecyclerView.setAdapter(firebaseAdapter1);

    return viewThree;
}

// get data from firebase DB
private void loadStoreDetails() {
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            list.clear();  // CLAER DATA BEFORE CHANGING. IF NOT DONE, IT WILL SHOW DUPLICATE DATA
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                list.add(ds.getValue(firebasestore.class));
            }
            firebaseAdapter1.notifyDataSetChanged();    // NOTIFY ADAPTER TO SHOW DATA IN VIEW WITHOUT RELOAD
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w("LogFragment", "loadLog:onCancelled", databaseError.toException());
        }
    };
    mdatabase.limitToLast(20).addValueEventListener(valueEventListener);

}

}
Kandarpa
  • 73
  • 9
  • check this https://stackoverflow.com/questions/21070095/clarify-the-firebase-connections SO post. Hope this will help you. – silwar Nov 27 '18 at 06:47
  • 1
    Thanks. It is of much help. @silwar – Kandarpa Nov 27 '18 at 08:28
  • As I read in the [docs](https://firebase.google.com/docs/firestore/rtdb-vs-firestore) Now days Realtime Database: "Scale to around 200,000 concurrent connections and 1,000 writes/second in a single database. Scaling beyond that requires sharding your data across multiple databases." – Fotios Tsakiris Jan 16 '20 at 15:06

1 Answers1

2

If there are no active listeners for a minute, the Firebase client will indeed close its connection to the server.

In your code you call loadStoreDetails attaches a listener with addValueEventListener from onCreate. Since you never remove that listener, it will stay active permanently from the moment ThreeFragment is created until the program exits.

To prevent this, and ensure the data is only synchronized (and the connection kept open) while the user has the fragment open, detach the listener in onDestroyView or onDestroy of the fragment.

For that, add a member field to the fragment:

ValueEventListener mFragmentListener;

Then keep a reference to the listener when you attach it:

mFragmentListener = mdatabase.limitToLast(20).addValueEventListener(valueEventListener);

And finally remove the listener when the fragment is destroyed:

@Override
public void onDestroyView() {
    mdatabase.limitToLast(20).removeEventListener(mFragmentListener);
}

On a separate note: the call to mdatabase.keepSynced(false); is not needed in your code, as that is the default behavior already.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your response. I have added the code. Since I am on firebase free subscription plan, I need to estimate the number of "Simultaneous connections", because only 100 connections are allowed at once. Moreover, if it exceeds 100, will the app crash, or it will show some error or it won't just load the data waiting for some connected user to get disconnected silently ? – Kandarpa Nov 27 '18 at 06:28
  • If there are already 100 connections, new incoming connections will be rejected. See https://stackoverflow.com/questions/50299797/firebase-simultaneous-connections-in-spark-plan/50300565#50300565 and many more from [here](https://stackoverflow.com/search?q=%5Bfirebase%5D+connections+user%3A209103). – Frank van Puffelen Nov 27 '18 at 14:21
  • I have seen that. But I wanted to know what does "rejected" means. Will it cause crash/show error or simply wont load data and app will work normally with data stored using disk persistence/other native data? – Kandarpa Nov 27 '18 at 15:50