Skip to content

refactor(rtdb, firestore): prevent paging from throwing ExecutionException #1988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ public Single<LoadResult<String, DataSnapshot>> loadSingle(@NonNull LoadParams<S
details).toException();
}
} catch (ExecutionException e) {
throw new Exception(e.getCause());
if (e.getCause() instanceof Exception) {
// throw the original Exception
throw (Exception) e.getCause();
}
// Only throw a new Exception when the original
// Throwable cannot be cast to Exception
throw new Exception(e);
}
}).subscribeOn(Schedulers.io()).onErrorReturn(LoadResult.Error::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import com.google.firebase.firestore.Source;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.paging.PagingState;
import androidx.paging.rxjava3.RxPagingSource;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class FirestorePagingSource extends RxPagingSource<PageKey, DocumentSnapshot> {
Expand All @@ -39,18 +38,24 @@ public Single<LoadResult<PageKey, DocumentSnapshot>> loadSingle(@NonNull LoadPar
}

return Single.fromCallable(() -> {
Tasks.await(task);
if (task.isSuccessful()) {
try {
Tasks.await(task);
QuerySnapshot snapshot = task.getResult();
PageKey nextPage = getNextPageKey(snapshot);
if (snapshot.getDocuments().isEmpty()) {
return toLoadResult(snapshot.getDocuments(), null);
}
return toLoadResult(snapshot.getDocuments(), nextPage);
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception) {
// throw the original Exception
throw (Exception) e.getCause();
}
// Only throw a new Exception when the original
// Throwable cannot be cast to Exception
throw new Exception(e);
}
throw task.getException();
}).subscribeOn(Schedulers.io())
.onErrorReturn(throwable -> new LoadResult.Error<>(throwable));
}).subscribeOn(Schedulers.io()).onErrorReturn(LoadResult.Error::new);
}

private LoadResult<PageKey, DocumentSnapshot> toLoadResult(
Expand Down