Kandarpa
Kandarpa

Reputation: 73

Simultaneous connections in firebase

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);

}

}

Upvotes: 0

Views: 398

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599071

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.

Upvotes: 2

Related Questions