[android][firestore] implement Document and Query snapshot serialize async tasks

This commit is contained in:
Salakar 2018-08-01 23:01:34 +01:00
parent 0061ad9888
commit 69fa5639c5
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package io.invertase.firebase.firestore;
import android.os.AsyncTask;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.google.firebase.firestore.DocumentSnapshot;
import java.lang.ref.WeakReference;
class DocumentSnapshotSerializeAsyncTask extends AsyncTask<Object, Void, WritableMap> {
private WeakReference<ReactContext> reactContextWeakReference;
private WeakReference<RNFirebaseFirestoreDocumentReference> referenceWeakReference;
DocumentSnapshotSerializeAsyncTask(
ReactContext context,
RNFirebaseFirestoreDocumentReference reference
) {
referenceWeakReference = new WeakReference<>(reference);
reactContextWeakReference = new WeakReference<>(context);
}
@Override
protected final WritableMap doInBackground(Object... params) {
DocumentSnapshot querySnapshot = (DocumentSnapshot) params[0];
try {
return FirestoreSerialize.snapshotToWritableMap(querySnapshot);
} catch (RuntimeException e) {
if (isAvailable()) {
reactContextWeakReference.get().handleException(e);
} else {
throw e;
}
return null;
}
}
@Override
protected void onPostExecute(WritableMap writableMap) {
// do nothing as overridden on usage
}
private Boolean isAvailable() {
return reactContextWeakReference.get() != null && referenceWeakReference.get() != null;
}
}

View File

@ -0,0 +1,47 @@
package io.invertase.firebase.firestore;
import android.os.AsyncTask;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.google.firebase.firestore.QuerySnapshot;
import java.lang.ref.WeakReference;
class QuerySnapshotSerializeAsyncTask extends AsyncTask<Object, Void, WritableMap> {
private WeakReference<ReactContext> reactContextWeakReference;
private WeakReference<RNFirebaseFirestoreCollectionReference> referenceWeakReference;
QuerySnapshotSerializeAsyncTask(
ReactContext context,
RNFirebaseFirestoreCollectionReference reference
) {
referenceWeakReference = new WeakReference<>(reference);
reactContextWeakReference = new WeakReference<>(context);
}
@Override
protected final WritableMap doInBackground(Object... params) {
QuerySnapshot querySnapshot = (QuerySnapshot) params[0];
try {
return FirestoreSerialize.snapshotToWritableMap(querySnapshot);
} catch (RuntimeException e) {
if (isAvailable()) {
reactContextWeakReference.get().handleException(e);
} else {
throw e;
}
return null;
}
}
@Override
protected void onPostExecute(WritableMap writableMap) {
// do nothing as overridden on usage
}
private Boolean isAvailable() {
return reactContextWeakReference.get() != null && referenceWeakReference.get() != null;
}
}