[android][database] switch to SparseArray for database references
This commit is contained in:
parent
c6773dc808
commit
82a2afd59b
|
@ -1,15 +1,11 @@
|
||||||
package io.invertase.firebase.database;
|
package io.invertase.firebase.database;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.Map;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
import android.telecom.Call;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import java.util.Map;
|
import android.util.SparseArray;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import com.facebook.react.bridge.Callback;
|
import com.facebook.react.bridge.Callback;
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
|
@ -29,26 +25,28 @@ import io.invertase.firebase.Utils;
|
||||||
public class RNFirebaseDatabaseReference {
|
public class RNFirebaseDatabaseReference {
|
||||||
private static final String TAG = "RNFirebaseDBReference";
|
private static final String TAG = "RNFirebaseDBReference";
|
||||||
|
|
||||||
private Query mQuery;
|
|
||||||
private int mRefId;
|
private int mRefId;
|
||||||
private String mPath;
|
private String mPath;
|
||||||
private Map<Integer, ChildEventListener> mChildEventListeners = new HashMap<>();
|
private Query mQuery;
|
||||||
private Map<Integer, ValueEventListener> mValueEventListeners = new HashMap<>();
|
|
||||||
private ReactContext mReactContext;
|
private ReactContext mReactContext;
|
||||||
|
private SparseArray<ChildEventListener> mChildEventListeners;
|
||||||
|
private SparseArray<ValueEventListener> mValueEventListeners;
|
||||||
|
|
||||||
public RNFirebaseDatabaseReference(final ReactContext context,
|
RNFirebaseDatabaseReference(final ReactContext context,
|
||||||
final FirebaseDatabase firebaseDatabase,
|
final FirebaseDatabase firebaseDatabase,
|
||||||
final int refId,
|
final int refId,
|
||||||
final String path,
|
final String path,
|
||||||
final ReadableArray modifiersArray) {
|
final ReadableArray modifiersArray) {
|
||||||
mReactContext = context;
|
|
||||||
mRefId = refId;
|
|
||||||
mPath = path;
|
mPath = path;
|
||||||
|
mRefId = refId;
|
||||||
|
mReactContext = context;
|
||||||
|
mChildEventListeners = new SparseArray<ChildEventListener>();
|
||||||
|
mValueEventListeners = new SparseArray<ValueEventListener>();
|
||||||
mQuery = this.buildDatabaseQueryAtPathAndModifiers(firebaseDatabase, path, modifiersArray);
|
mQuery = this.buildDatabaseQueryAtPathAndModifiers(firebaseDatabase, path, modifiersArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addChildEventListener(final int listenerId, final String eventName) {
|
void addChildEventListener(final int listenerId, final String eventName) {
|
||||||
if (!mChildEventListeners.containsKey(listenerId)) {
|
if (mChildEventListeners.get(listenerId) != null) {
|
||||||
ChildEventListener childEventListener = new ChildEventListener() {
|
ChildEventListener childEventListener = new ChildEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
|
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
|
||||||
|
@ -84,6 +82,7 @@ public class RNFirebaseDatabaseReference {
|
||||||
handleDatabaseError(listenerId, error);
|
handleDatabaseError(listenerId, error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
mChildEventListeners.put(listenerId, childEventListener);
|
mChildEventListeners.put(listenerId, childEventListener);
|
||||||
mQuery.addChildEventListener(childEventListener);
|
mQuery.addChildEventListener(childEventListener);
|
||||||
Log.d(TAG, "Added ChildEventListener for refId: " + mRefId + " listenerId: " + listenerId);
|
Log.d(TAG, "Added ChildEventListener for refId: " + mRefId + " listenerId: " + listenerId);
|
||||||
|
@ -92,8 +91,8 @@ public class RNFirebaseDatabaseReference {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addValueEventListener(final int listenerId) {
|
void addValueEventListener(final int listenerId) {
|
||||||
if (!mValueEventListeners.containsKey(listenerId)) {
|
if (mValueEventListeners.get(listenerId) != null) {
|
||||||
ValueEventListener valueEventListener = new ValueEventListener() {
|
ValueEventListener valueEventListener = new ValueEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||||
|
@ -106,6 +105,7 @@ public class RNFirebaseDatabaseReference {
|
||||||
handleDatabaseError(listenerId, error);
|
handleDatabaseError(listenerId, error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
mValueEventListeners.put(listenerId, valueEventListener);
|
mValueEventListeners.put(listenerId, valueEventListener);
|
||||||
mQuery.addValueEventListener(valueEventListener);
|
mQuery.addValueEventListener(valueEventListener);
|
||||||
Log.d(TAG, "Added ValueEventListener for refId: " + mRefId + " listenerId: " + listenerId);
|
Log.d(TAG, "Added ValueEventListener for refId: " + mRefId + " listenerId: " + listenerId);
|
||||||
|
@ -201,7 +201,7 @@ public class RNFirebaseDatabaseReference {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean hasListeners() {
|
boolean hasListeners() {
|
||||||
return !mChildEventListeners.isEmpty() || !mValueEventListeners.isEmpty();
|
return mChildEventListeners.size() > 0 || mValueEventListeners.size() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanup() {
|
public void cleanup() {
|
||||||
|
@ -211,16 +211,18 @@ public class RNFirebaseDatabaseReference {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeChildEventListener(Integer listenerId) {
|
private void removeChildEventListener(Integer listenerId) {
|
||||||
ChildEventListener listener = mChildEventListeners.remove(listenerId);
|
ChildEventListener listener = mChildEventListeners.get(listenerId);
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
mQuery.removeEventListener(listener);
|
mQuery.removeEventListener(listener);
|
||||||
|
mChildEventListeners.delete(listenerId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeValueEventListener(Integer listenerId) {
|
private void removeValueEventListener(Integer listenerId) {
|
||||||
ValueEventListener listener = mValueEventListeners.remove(listenerId);
|
ValueEventListener listener = mValueEventListeners.get(listenerId);
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
mQuery.removeEventListener(listener);
|
mQuery.removeEventListener(listener);
|
||||||
|
mValueEventListeners.delete(listenerId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue