8
8
import com .google .firebase .firestore .Source ;
9
9
10
10
import java .util .List ;
11
- import java .util .concurrent .Callable ;
11
+ import java .util .concurrent .ExecutionException ;
12
12
13
13
import androidx .annotation .NonNull ;
14
14
import androidx .annotation .Nullable ;
15
15
import androidx .paging .PagingState ;
16
16
import androidx .paging .rxjava3 .RxPagingSource ;
17
17
import io .reactivex .rxjava3 .core .Single ;
18
- import io .reactivex .rxjava3 .functions .Function ;
19
18
import io .reactivex .rxjava3 .schedulers .Schedulers ;
20
19
21
20
public class FirestorePagingSource extends RxPagingSource <PageKey , DocumentSnapshot > {
@@ -39,18 +38,24 @@ public Single<LoadResult<PageKey, DocumentSnapshot>> loadSingle(@NonNull LoadPar
39
38
}
40
39
41
40
return Single .fromCallable (() -> {
42
- Tasks . await ( task );
43
- if ( task . isSuccessful ()) {
41
+ try {
42
+ Tasks . await ( task );
44
43
QuerySnapshot snapshot = task .getResult ();
45
44
PageKey nextPage = getNextPageKey (snapshot );
46
45
if (snapshot .getDocuments ().isEmpty ()) {
47
46
return toLoadResult (snapshot .getDocuments (), null );
48
47
}
49
48
return toLoadResult (snapshot .getDocuments (), nextPage );
49
+ } catch (ExecutionException e ) {
50
+ if (e .getCause () instanceof Exception ) {
51
+ // throw the original Exception
52
+ throw (Exception ) e .getCause ();
53
+ }
54
+ // Only throw a new Exception when the original
55
+ // Throwable cannot be cast to Exception
56
+ throw new Exception (e );
50
57
}
51
- throw task .getException ();
52
- }).subscribeOn (Schedulers .io ())
53
- .onErrorReturn (throwable -> new LoadResult .Error <>(throwable ));
58
+ }).subscribeOn (Schedulers .io ()).onErrorReturn (LoadResult .Error ::new );
54
59
}
55
60
56
61
private LoadResult <PageKey , DocumentSnapshot > toLoadResult (
0 commit comments