RxJava Having trouble Converting one list to another list

Sahil Patel

I am trying to convert Flowable<List<TaskEntity>> to Flowable<List<Task>> but something is wrong. To understand the problem I tried with converting a simpler list and it is working fine. When I try to apply the same logic to my actual problem, it is not working.


This logic is giving me expected output. [No.1 No.2 No.3]

Flowable.fromArray(Arrays.asList(1,2,3))
        .flatMapIterable(ids->ids)
        .map(s->"No. "+s)
        .toList()
        .toFlowable()
        .subscribe(
                t -> Log.d(TAG, "getAllActiveTasks: "+t)
        );

This logic is not working . It prints Nothing

    mTaskDao.getAllTasks(STATE_ACTIVE)
            .flatMapIterable(task -> task)
            .map(Task::create)
            .toList()
            .toFlowable()

            .subscribe(
                    t -> Log.d(TAG, "getAllActiveTasks: "+t)
            );

Edit 1 This is how Task.create() looks like.

     public static Task create(TaskEntity eTask) {
         Task task = new Task(eTask.getTaskId(), eTask.getTaskTitle(), eTask.getTaskStatus());
         task.mTaskDescription = eTask.getTaskDescription();
         task.mCreatedAt = eTask.getCreatedAt();
         task.mTaskDeadline = eTask.getTaskDeadline();
         return task;
     }

Solution

As mentioned in the comments, toList() can only work if emitting source has finite number of items to emit. Since Flowable from Dao method contains an infinite stream of objects, toList() was not being used correctly by me.

Checkout this comment for the exact way to solve this problem. https://stackoverflow.com/a/50318832/4989435

akarnokd

toList requires a finite source but getAllTasks is likely infinite, which is unfortunately quite typical from DAOs backed by Android databases. Change the getAllTasks to Single, use take(1), use timeout(), or use flatMap(Observable.fromIterable().map().toList()) instead of flatMapIterable.

I want to receive any updates made to tasks in db.

In this case, you need the latter option:

mTaskDao.getAllTasks(STATE_ACTIVE)
        .flatMapSingle(task -> 
             Observable.fromIterable(task)
            .map(Task::create)
            .toList()
        )
        .subscribe(
                t -> Log.d(TAG, "getAllActiveTasks: "+t)
        );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Having trouble converting a list of strings into numbers

Convert one type of list to another RXJava? (Javascript map equivalent)

List comprehension - converting strings in one list, to integers in another

Having trouble creating a list based on function arguments

Converting all letters in a document from one list to another in Python

Having trouble calculating the value of a list of classes in Javascript

Having trouble converting if into cond

Converting list from one class to another

Having trouble adding to a List that exists

Having trouble understanding the definition of a linked list

Having trouble aligning a list and a video horizontally

Converting user input to string, having trouble with more than one letter

Having trouble with testing if a thing in a list exists in python

retrieve HashMap list into another list , one key having many values

Having trouble getting list.js to work

Trouble converting string into List<int>

Having trouble copying from one workbook to another

Having trouble casting string to list in racket

Having trouble with generator in list comprehension

Converting list of lists to one list

Having trouble loading "neighbours" of a specific tile into a List

Having trouble with array conversion into list in HTML

Converting list of one class to list of another class?

Having Trouble Removing Duplicates From A List

I have a list in one class and I'm having trouble accessing it from another class C#

Having Trouble printing a doubly linked list

Having trouble removing specific values of a list of strings

Having a heading on one side and a list on another

Having trouble extracting values from a nested list and converting to dataframe