Android Kotlin RXKotlin Room - Error Kotlin.unit

64
July 28, 2021, at 7:40 PM

I want to make a simple insert data to room database with looping an array. I used RXKotlin to iterate the array I have an array like this :

fun defaultDataCategory() : ArrayList<CategoryModel>{
        var cat: CategoryModel
        var catArrayList: ArrayList<CategoryModel> = ArrayList(0)
        val date: Int = Calendar.DATE
        val formatedDate = SimpleDateFormat("yyyy-MM-dd").format(Date())
        val formatedTime = SimpleDateFormat("HH:mm").format(Date())
        val DateTime = "$formatedDate  $formatedTime"
        catArrayList.add(
            CategoryModel(
                1,
                "Personal",
                true,
                "Red",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system"
            )
        )
        catArrayList.add(
            CategoryModel(
                2,
                "Work",
                true,
                "Blue",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system"
            )
        )
        catArrayList.add(
            CategoryModel(
                3,
                "Home",
                true,
                "Purple",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system"
            )
        )
        catArrayList.add(
            CategoryModel(
                4,
                "Learn",
                true,
                "Yellow",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system",
                Converter.toDate(Calendar.getInstance().timeInMillis),
                "system"
            )
        )
       return catArrayList
    }

And I loop an array with RXKotlin like this

var catArrayList: ArrayList<CategoryModel> = DefaultData.defaultDataCategory()
        catArrayList.toObservable()
            .subscribeBy(  // named arguments for lambda Subscribers
                onNext = { homeViewModel.insertCategory(it) },
                onError = { Log.e("error insert=", it.printStackTrace().toString()) },
                onComplete = { Log.e("complete insert=","complete insert") }
            )

And I got an error "kotlin.unit" .

Error RXKotlin

How to iterate an array with RXKotlin and insert to room database?

Answer 1

Try

Observable.fromIterable(catArrayList).subscribeBy {...}

READ ALSO
Does Lower value in rows column of explain query means faster query

Does Lower value in rows column of explain query means faster query

I have a table, with about 10M entries, A select query is too slow, So I tried adding a composite index to the table, explain query shows lesser number of rows compared after adding the composite indexBut on running the actual query, the test table in which the index is added sometimes...

81
How do I print user information once logged in?

How do I print user information once logged in?

I've added a login system to my website but it seems the information isn't properly being stored in the sessionWhen I try to open up the destination page, I am trying to echo the user_id, but I get the error: "Trying to access array offset on value of type null"

63
(discord.js + document) Replace new lines with &lt;br&gt;

(discord.js + document) Replace new lines with <br>

I am creating a ticket bot with discordjs

51
Using Cropper.js with only javascript, my below script codes are conflicting with each other

Using Cropper.js with only javascript, my below script codes are conflicting with each other

I'am trying to make a custom mockup generatorWith below this script code i can mockup in the browser without using ajax or something else

68