[android][firestore] implement Document and Query snapshot serialize async tasks
This commit is contained in:
parent
0061ad9888
commit
69fa5639c5
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue