1
votes

I have the following situation:

  • I am using the RxKotlin extensions for detecting clicks in the buttons of my activity
  • I am using Room for inserting records in a database

This is the code related in my activity:

button.clicks()
            .flatMap {
                val list = mutableListOf<Answer>()
                val date = Date()
                list.add(Answer("some placeholder info")) 
                list.add(Answer("Another placeholder info")) 
                Observable.fromArray(list)
            }
            .map {
                upsertStatusQuestionsViewModel.insertMultipleAnswers(it)
            }.subscribe {
                // it here is an object Maybe<Answer>
            }

And this is the code of the ViewModel:

fun insertMultipleAnswers(answers: List<Answer>) = database.answerDao()
                                                      .createMultiple(answers.toList())
                                                      .subscribeOn(Schedulers.io())
                                                      .observeOn(AndroidSchedulers.mainThread())

I would like to show some information about the answer inserted in the database, for that reason, I need to get the Answer object in my subscription. However, I don't know which operator can I use for achieving that the it object in the subscription is of class Answer, instead of Maybe<Answer>.

Thanks a lot for your help!

1

1 Answers

0
votes

If anyone stumbles with this, the solution is to parse the Maybe to an Observable, as Observable implements ObservableSource, so my code is now something like this:

upsert_status_questions_confirm.clicks()
            .map {
                val list = mutableListOf<Answer>()
                list.add(Answer("Some placeholder"))
                list.add(Answer("Another placeholder"))
                list
            }.flatMap {
                upsertStatusQuestionsViewModel.insertMultipleAnswers(*it.toTypedArray())
            }.subscribe({
                // Success...
            }, {
                // Error...
            })

And in the ViewModel:

fun insertMultipleAnswers(vararg answers: Answer) = database.answerDao()
                                               .createMultiple(answers.toList())
                                               .toObservable()
                                               .subscribeOn(Schedulers.io())
                                               .observeOn(AndroidSchedulers.mainThread())