Issue
I have an insert method that uses Asynctask
to set a new item in the Room
database. Can I get rid of the InsertContactAsyncTask
class and switch to using RxJava
? I can't find such examples on the Internet
method insert
:
override fun insert(contact: Contact) {
InsertContactAsyncTask().execute(contact)
}
AsyncTask class
:
class InsertContactAsyncTask: AsyncTask<Contact, Unit, Unit>() {
private val contactDao: ContactDao? = null
override fun doInBackground(vararg param: Contact) {
contactDao?.insert(param[0])
}
}
Solution
fun insert(contact:Contact) : Completable
Then somewhere you call this method
contactDao.insert()
.subscribeOn(Schedulers.IO)
.observeOn(AndroidSchedulers.Main)
.subscribe{Log.d("test","insertcompleted")
//do something when insert completed
}
or just place insert into completable like this
fun insertCompletable(contact:Contact){
Completable.fromAction{contactDao.insert}
}
and subscribe to it the same way as previous
Answered By - Ilia Kuzmin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.