Insert a List Into Room Database Using RxJava - android

I have a list of Items I want to map and then insert into a Room table:
Room
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg projectLocal: ProjectLocal): Completable
The FIRST approach to save data:
Observable.fromIterable(remoteProjects)
.map { project ->
...
mProjectMapper.mapRemoteToLocal(project)
}
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe {
mProjectRepository.saveProject(it)
}
As you may see I'm observing on -> main thread and subscribing on -> io
The Second approach to save data:
remoteProjects.forEach { remote ->
...
val project = mProjectMapper.mapRemoteToLocal(remote)
mProjectRepository.saveProject(project)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe()
}
Which one makes more sense? Is there any better way to save all this data inside a Room database using RxJava?

I think this is what #Mwasz means:
Observable.fromIterable(remoteProjects)
.map { project ->
mProjectMapper.mapRemoteToLocal(project)
}
.toList()
.flatMapCompletable {
mProjectRepository.saveProject(it)
.subscribeOn(Schedulers.io())
}
.subscribe()
You could also use reduce or collect instead of toList but toList() is the simplest.

Related

Room RxJava observable triggered multiple times on insert

I'm having a weird problem with my repository implementation. Every time I call my function that's supposed to get data from the database and update the database with a network call, I receive multiple results from my database observer.
override fun getApplianceControls(
serialNumber: SerialNumber
): Flowable<ApplianceControlState> {
val subject = BehaviorProcessor.create<ApplianceControlState>()
controlsDao.get(serialNumber.serial)
.map { controls ->
ApplianceControlState.Loaded(controls.toDomainModel())
}
.subscribe(subject)
controlApi.getApplianceControls(serialNumber.serial)
.flatMapObservable<ApplianceControlState> { response ->
val entities = response.toEntity(serialNumber)
// Store the fetched controls on the database.
controlsDao.insert(entities).andThen(
// Return an empty observable because the db will take care of emitting latest values.
Observable.create { }
)
}
.onErrorResumeNext { error: Throwable ->
Observable.create { emitter -> emitter.onNext(ApplianceControlState.Error(error)) }
}
.subscribeOn(backgroundScheduler)
.subscribe()
return subject.distinctUntilChanged()
}
#Dao
interface ApplianceControlsDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(controls: List<TemperatureControlEntity>): Completable
#Query("SELECT * FROM control_temperature WHERE serial = :serial")
fun get(serial: String): Flowable<List<TemperatureControlEntity>>
}
Basically, if I call getApplianceControls once, I get desired result. Then I call again, with another serial number, which is empty and I get the empty array. But then I call a third time, but with the same serial number as the first time and I get a mix of correct results and empty array after the insert call is made.
Like this:
1st call, to serial number "123" -> Loaded([control1, control2, control3])
2nd call, to serial number "000" -> Loaded([])
3rd call, to serial number "123" -> Loaded([control1, control2, control3]), Loaded([]), Loaded([control1, control2, control3])
If I remove the db insert from the api response, it works fine. Everything weird occurs after insert is called.
Edit: getApplianceControls() is called from the ViewModel.
fun loadApplianceControls(serialNumber: SerialNumber) {
Log.i("Loading appliance controls")
applianceControlRepository.getApplianceControls(serialNumber)
.subscribeOn(backgroundScheduler)
.observeOn(mainScheduler)
.subscribeBy(
onError = { error ->
Log.e("Error $error")
},
onNext = { controlState ->
_controlsLiveData.value = controlState
}
).addTo(disposeBag)
}
As i mention in comment you have 2 subscriptions that are not unsubscribed anywhere, it could cause memory leak (it doesn't dispose when subject is disposed), also with such implementation you ignore API errors.
i'd try to change it to:
override fun getApplianceControls(serialNumber: SerialNumber): Flowable<ApplianceControlState> {
val dbObservable = controlsDao.get(serialNumber.serial)
.map { controls ->
ApplianceControlState.Loaded(controls.toDomainModel())
}
val apiObservable = controlApi.getApplianceControls(serialNumber.serial)
.map { response ->
val entities = response.toEntity(serialNumber)
// Store the fetched controls on the database.
controlsDao.insert(entities).andThen( Unit )
}
.toObservable()
.startWith(Unit)
return Observables.combineLatest(dbObservable, apiObservable) { dbData, _ -> dbData }
// apiObservable emits are ignored, but it will by subscribed with dbObservable and Errors are not ignored
.onErrorResumeNext { error: Throwable ->
Observable.create { emitter -> emitter.onNext(ApplianceControlState.Error(error)) }
}
.subscribeOn(backgroundScheduler)
//observeOn main Thread
.distinctUntilChanged()
}
I'm not sure if it solves the original issue. But if so - the issue is in flatMapObservable
ALSO would be useful to see controlApi.getApplianceControls() implementation.

Convert two RxJava requests to one request

I have two requests that I execute using RxJava. They should run one after the other. This is what I have at the current moment :
fun showPhotos() {
_liveData.postValue(Resource.Loading())
compositeDisposable.add(useCase.getPhotos()
.subscribe({
showPosts(it)
}) {
_liveData.postValue(Resource.Failure(it.localizedMessage))
Timber.e(it)
})
}
private fun showPosts(networkPhotos: List<NetworkPhoto>) {
compositeDisposable.add(useCase.getPost()
.subscribe({ networkPosts ->
_liveData.postValue(
Resource.Success(PostAndImages(networkPosts, networkPhotos).asDomaineModel())
)
}) {
_liveData.postValue(Resource.Failure(it.localizedMessage))
Timber.e(it)
})
}
Is there any solution that I can make one RxJava call instead of executing two requests sequentially ?
If you need to run 2 queries sequentially, you can use the flatMap operator.
data class RequestWrapper(var photos: YourType? = null, var networkPosts : YourType? = null)
fun sequentiallyRequest(){
val requestWrapper = RequestWrapper()
useCase.getPhotos()
.map{requestWrapper.photos= it}
.flatMap{useCase.getPost()}
.map{requestWrapper.networkPosts = it}
.subscribe({
_liveData.postValue(
Resource.Success(PostAndImages(requestWrapper.networkPosts, networkPhotos).asDomaineModel())
)
})
Or, use operator zip. But in this case, the requests will be executed in parallel.
Single.zip(
useCase.getPhotos(),
useCase.getPost(),
Pair::new)
.subscribe(pair -> {
showPosts(pair.first)
_liveData.postValue(
Resource.Success(PostAndImages(pair.second, networkPhotos).asDomaineModel())
}
You can use .flatMap() if one depends on another or .zip() if you just need to run them both at once and merge them after both completed
You may using zip. Queries will work in order.
Single.Zip(
useCase.getPhotos().subscribeOn(Schedulers.newThread()),
useCase.getPost().subscribeOn(Schedulers.newThread()),
BiFunction { photos: ????, posts: ???? -> Pair(photos, posts) }
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
it.first is photos
it.second is posts
},{
error
})

Room getting ConcurrentModificationException

I am facing an issue while inserting data to Android table. Here is my Dao functions:
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(freight: Foo)
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(freights: MutableList<Foo>)
Here is how it is invoke:
Observable.fromCallable {
db.fooDao().insert(it)
}
}
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe {
Logger.d("Inserted ${it} users from API in DB...")
}
Exception I am getting:
Caused by: java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at com.blockgrain.blockgrain.dbmanager.repository.FooRepository$insertDataInDb$1.call(FooRepository.kt:76)
I have created other tables with same logic they are working fine but this one is failing . Please let me know what went wrong.
Update :
Foo.kt
override fun get(): Observable<MutableList<Foo>> {
val observable = getDataFromDb()
return observable.flatMap {
if (it.isEmpty())
getDataFromApi()
else
observable
}
}
override fun getDataFromApi(): Observable<MutableList<Foo>> {
return WebService.createWithAuth().getFooFromWeb()
.doOnNext {
Logger.d(" Dispatching ${it} users from API...")
Observable.fromCallable {
db.fooDao().insert(it)
}
}
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe {
Logger.d("Inserted ${it} users from API in DB...")
}
}
}
As per the given code, It is not directly clear how the array list modification is being called resulting into Caused by: java.util.ConcurrentModificationException .
My guess is, multiple operations are being performed on same list at a time.
Your insert list method in dao is accepting MutableList<Foo> change it to List<Foo> as Room doesn't need mutable list. like this,
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(freights: List<Foo>)
I would recommend to copy the array list to another list before doing any operations on the list like this
// Before performing any operation on list
var newList:List<Foo> = ArrayList<Foo>(otherList)
// Perform operation on newList - for ex.
db.insert(newList)
There is another solution for ArrayList if you want to use it concurrently with CopyOnWriteArrayList. But this will result into significant modification in existing in code. So I would recommend to go with first option.

Android and Room: inserting works, querying does not

I am using Room in a project and I have the following DAO interface:
#Dao
interface BalanceDao {
#Query("SELECT * FROM balance")
fun getAllBalances(): Flowable<List<BalanceDataModel>>
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertBalanceList(balanceDataModelList: List<BalanceDataModel>): List<Long>
}
Insert works fantastically, but the getAllBalances() method does not work, since it does not retrieve any row. I extracted the DB after the insertion and I can see all the rows there; SELECT * from balance works perfectly when locally executed to the extracted DB with a desktop app.
I also tried to change the return type of getAllBalances() from Flowable<List<BalanceDataModel>> to Single<List<BalanceDataModel>> but the same keeps happening: no results.
I have a PortfolioManager, from which I call the following method and I pass the observer and the owner from my Fragment.
fun getAllCoins(owner: LifecycleOwner, observer: Observer<List<Balance>>) {
portfolioViewModel
.balanceListLiveData
.observe(owner, observer)
return portfolioViewModel.getPortfolioCoins()
}
Then in the PortfolioManager, I have access to a ViewModel, from which I call the following method:
fun getPortfolioCoins() {
coinRepository
.getBalanceListPortfolio()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onSuccess = {
balanceListLiveData.postValue(it)
})
}
And in the repository class, I have this:
fun getBalanceListPortfolio(): Single<List<Balance>> {
val converter = BalanceDataModelConverter()
val balanceList = mutableListOf<Balance>()
coinDatabase.balanceDao()
.getAllBalances()
.toObservable()
.flatMapIterable { t: List<BalanceDataModel> -> t }
.map { t: BalanceDataModel ->
{
val a: Balance = converter.fromFirstToSecond(t)
balanceList.add(a)
}
}
return Single.just(balanceList.toList())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
Anybody knows what could be wrong? Thanks a lot in advance!
I just saw your recent edit. I think your problem is you are returning an empty list in getBalanceListPortfolio. This observable was not subscribed upon.
coinDatabase.balanceDao()
.getAllBalances()
.toObservable()
.flatMapIterable { t: List<BalanceDataModel> -> t }
.map { t: BalanceDataModel ->
{
val a: Balance = converter.fromFirstToSecond(t)
balanceList.add(a)
}
}
I suggest you convert this to list and return this observable (something like this, cant try to compile. I have no working env right now).
return coinDatabase.balanceDao()
.getAllBalances()
.toObservable()
.flatMapIterable { t: List<BalanceDataModel> -> t }
.map { t: BalanceDataModel -> converter.fromFirstToSecond(t) }
.toList()
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
This is wrapped as an observable to you might want to change your type to Observable (instead of single) first, just to test. Let me know the results.

Room + RxJava2 case with infinite loop

I'm using the new Android persistance lib, Room, with RxJava2. The following code is causing an infinite loop. If I comment out the line that updates the user in the second observable it works fine. If I leave it there, the onNext method of the first observable will be called on and on again.
Does Room requery the table when an entity is updated? If so, why is it publishing the message again on the same stream? Is this intended behavior? Is it a bug in the library?
val userDao = HeyHeyApp.database.userDao();
userDao.getAll()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ listOfUsers ->
if (!listOfUsers.isEmpty()) {
HeyHeyApp.currentUser = listOfUsers.first()
HeyHeyApp.currentUser.fcmDeviceId = getDeviceId()
Single.fromCallable({
HeyHeyApp.database.userDao()
.updateUser(HeyHeyApp.currentUser)
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ _ ->
})
}
})
When you subscribes for userDao.getAll() event - Room will trigger your observer onNext() method each time when databases data is changed. And next in your onNext() method you change the data in database:
Single.fromCallable({
HeyHeyApp.database.userDao()
.updateUser(HeyHeyApp.currentUser)
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ _ ->
})
and after that Room call your onNext() method again... and so on.

Categories

Resources