[android][database] switch to SparseArray for database references

This commit is contained in:
Salakar 2017-07-12 13:48:21 +01:00
parent c6773dc808
commit 82a2afd59b
1 changed files with 27 additions and 25 deletions

View File

@ -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<Integer, ChildEventListener> mChildEventListeners = new HashMap<>();
private Map<Integer, ValueEventListener> mValueEventListeners = new HashMap<>();
private Query mQuery;
private ReactContext mReactContext;
private SparseArray<ChildEventListener> mChildEventListeners;
private SparseArray<ValueEventListener> mValueEventListeners;
public RNFirebaseDatabaseReference(final ReactContext context,
RNFirebaseDatabaseReference(final ReactContext context,
final FirebaseDatabase firebaseDatabase,
final int refId,
final String path,
final ReadableArray modifiersArray) {
mReactContext = context;
mRefId = refId;
mPath = path;
mRefId = refId;
mReactContext = context;
mChildEventListeners = new SparseArray<ChildEventListener>();
mValueEventListeners = new SparseArray<ValueEventListener>();
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)) {