These are chat archives for ReactiveX/RxJava

15th
Mar 2017
DavidMihola
@DavidMihola
Mar 15 2017 09:41
@wilsoncastiblanco I'd like to propose a different solution to your problem:
Flowable.fromIterable(letters)
    .reduce((intermediateResult, nextLetter) -> intermediateResult + "_" + nextLetter)
    .blockingGet();
Haris Kljajić
@devharis
Mar 15 2017 12:40
public Observable<List<Assignment>> query(){
    Log.d(TAG, "Fetching data remotely...");

    return mRetrofit.create(AssignmentService.class).GET("token")
             .map(assignmentDTO -> mTransformer.transform(assignmentDTO, Assignment.class))
             .flatMap(mAssignmentLocalDataStore::add);
}
Is it possible to get a list of items and iterate through them to transform them to another type of POJO. Then take the new list of POJO's and use flatMap to add them to a database? Like in the example above?
Dorus
@Dorus
Mar 15 2017 12:41
yes
Haris Kljajić
@devharis
Mar 15 2017 12:41
I don't seem to be able to return a Observable<List<Assignment>>
DavidMihola
@DavidMihola
Mar 15 2017 12:41
What does mAssignmentLocalDataStore::add look like?
Haris Kljajić
@devharis
Mar 15 2017 12:42
    @Override
    public Observable<List<Assignment>> add(List<Assignment> assignments) {
        return Observable.create(subscriber -> {
            try {
                mDaoSession.insert(assignments);
                subscriber.onNext((List<Assignment>) assignments);
            } finally {
                subscriber.onCompleted();
            }
        });
Like this @DavidMihola

I got it to work like this before

    public Observable<List<Assignment>> query(){
        Log.d(TAG, "Fetching data remotely...");

        return mRetrofit.create(AssignmentService.class).GET("token")
                .flatMap(mAssignmentLocalDataStore::add);
    }

The difference now is that I need to map it to another type of list before i do the add

DavidMihola
@DavidMihola
Mar 15 2017 12:52
1.) Since add just seems to return list like it was entered - could you use doOnNext instead?
2.) And mTransformer.transform does return a List<Assignment>, too?
3.) You seem to be using Retrolamda - does it work if you write out the whole anonymous class in all cases?
Haris Kljajić
@devharis
Mar 15 2017 12:57
Ill try! Thanks for the advice.
mTransform takes a single AssignmentDTO from the list and transforms it into a Assignment
DavidMihola
@DavidMihola
Mar 15 2017 13:08
Then where do you get the List from? If transform operates on single Assignments instead of List<Assignment> then you seem to have an Observable<Assignment> and not an Observable<List<Assignment>>...
Haris Kljajić
@devharis
Mar 15 2017 13:47
Yeah, that's what I'm trying to achieve. I get a list from the API, iterate on that list to transform each assignmentDTO to a assignment. Then pass it to the add method and finally return the list as observable. I'm totally new to RxJava so trying to figure out a solution.
Haris Kljajić
@devharis
Mar 15 2017 15:21
        return mRetrofit.create(AssignmentService.class).GET("token")
                .flatMapIterable(assignmentDTOs -> assignmentDTOs)
                .map(assignmentDTO -> mTransformer.transform(assignmentDTO, Assignment.class))
                .doOnNext(mAssignmentLocalDataStore::add)
                .toList();
Got something like that to work, hehe. I guess I'll find a improvment when I get more comfortable with RxJava...