diff --git a/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java b/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java index 8262c818..d1374514 100644 --- a/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java +++ b/android/src/main/java/io/invertase/firebase/database/RNFirebaseDatabaseReference.java @@ -1,15 +1,11 @@ package io.invertase.firebase.database; -import java.util.HashMap; -import java.util.HashSet; +import java.util.Map; import java.util.List; -import android.support.annotation.Nullable; -import android.telecom.Call; import android.util.Log; - -import java.util.Map; -import java.util.Set; +import android.support.annotation.Nullable; +import android.util.SparseArray; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Arguments; @@ -29,26 +25,28 @@ import io.invertase.firebase.Utils; public class RNFirebaseDatabaseReference { private static final String TAG = "RNFirebaseDBReference"; - private Query mQuery; private int mRefId; private String mPath; - private Map mChildEventListeners = new HashMap<>(); - private Map mValueEventListeners = new HashMap<>(); + private Query mQuery; private ReactContext mReactContext; + private SparseArray mChildEventListeners; + private SparseArray mValueEventListeners; - public RNFirebaseDatabaseReference(final ReactContext context, - final FirebaseDatabase firebaseDatabase, - final int refId, - final String path, - final ReadableArray modifiersArray) { - mReactContext = context; - mRefId = refId; + RNFirebaseDatabaseReference(final ReactContext context, + final FirebaseDatabase firebaseDatabase, + final int refId, + final String path, + final ReadableArray modifiersArray) { mPath = path; + mRefId = refId; + mReactContext = context; + mChildEventListeners = new SparseArray(); + mValueEventListeners = new SparseArray(); mQuery = this.buildDatabaseQueryAtPathAndModifiers(firebaseDatabase, path, modifiersArray); } - public void addChildEventListener(final int listenerId, final String eventName) { - if (!mChildEventListeners.containsKey(listenerId)) { + void addChildEventListener(final int listenerId, final String eventName) { + if (mChildEventListeners.get(listenerId) != null) { ChildEventListener childEventListener = new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { @@ -84,6 +82,7 @@ public class RNFirebaseDatabaseReference { handleDatabaseError(listenerId, error); } }; + mChildEventListeners.put(listenerId, childEventListener); mQuery.addChildEventListener(childEventListener); Log.d(TAG, "Added ChildEventListener for refId: " + mRefId + " listenerId: " + listenerId); @@ -92,8 +91,8 @@ public class RNFirebaseDatabaseReference { } } - public void addValueEventListener(final int listenerId) { - if (!mValueEventListeners.containsKey(listenerId)) { + void addValueEventListener(final int listenerId) { + if (mValueEventListeners.get(listenerId) != null) { ValueEventListener valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { @@ -106,6 +105,7 @@ public class RNFirebaseDatabaseReference { handleDatabaseError(listenerId, error); } }; + mValueEventListeners.put(listenerId, valueEventListener); mQuery.addValueEventListener(valueEventListener); Log.d(TAG, "Added ValueEventListener for refId: " + mRefId + " listenerId: " + listenerId); @@ -201,7 +201,7 @@ public class RNFirebaseDatabaseReference { } boolean hasListeners() { - return !mChildEventListeners.isEmpty() || !mValueEventListeners.isEmpty(); + return mChildEventListeners.size() > 0 || mValueEventListeners.size() > 0; } public void cleanup() { @@ -211,16 +211,18 @@ public class RNFirebaseDatabaseReference { } private void removeChildEventListener(Integer listenerId) { - ChildEventListener listener = mChildEventListeners.remove(listenerId); + ChildEventListener listener = mChildEventListeners.get(listenerId); if (listener != null) { mQuery.removeEventListener(listener); + mChildEventListeners.delete(listenerId); } } private void removeValueEventListener(Integer listenerId) { - ValueEventListener listener = mValueEventListeners.remove(listenerId); + ValueEventListener listener = mValueEventListeners.get(listenerId); if (listener != null) { mQuery.removeEventListener(listener); + mValueEventListeners.delete(listenerId); } } @@ -271,7 +273,7 @@ public class RNFirebaseDatabaseReference { query = query.orderByChild(key); } } else if ("limit".equals(type)) { - int limit = ((Double)modifier.get("limit")).intValue(); + int limit = ((Double) modifier.get("limit")).intValue(); if ("limitToLast".equals(name)) { query = query.limitToLast(limit); } else if ("limitToFirst".equals(name)) {