如何解决使用Room插入本地数据库无效-Android
当我从API提取数据(有效)时,我想将其插入DB,由于某种原因,它不起作用。我正在使用Room库。 (使用Koin DI库)
这是我的代码:
用于从API提取数据的服务:
class PlayerServiceImplFb : PlayerService {
override fun fetchAllPlayers(): Observable<List<PlayerFb>> {
return Observable.create<List<PlayerFb>> {
val ref = FirebaseDatabase.getInstance().getReference("/players")
val matchList : MutableList<PlayerFb> = mutablelistof()
ref.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onCancelled(error: DatabaseError) {
Todo("Not yet implemented")
}
override fun onDataChange(snapshot: DataSnapshot) {
val children = snapshot.children
children.forEach {
Log.d("PlayerServiceImpl","Added player ${it.getValue(PlayerFb::class.java)} to list")
matchList.add(it.getValue(PlayerFb::class.java)!!)
}
it.onNext(matchList)
it.onComplete()
}
})
}
}
}
DAO:
@Dao
abstract class PlayerDao {
//DELETE ALL AND INSERT PLAYERS
@Transaction
open fun deleteAndInsertAll(entities: List<PlayerEntity>) {
deleteall()
insertAll(entities).blockingAwait()
}
@Insert( onConflict = OnConflictStrategy.REPLACE )
abstract fun insertAll(entities: List<PlayerEntity>): Completable
@Query("DELETE FROM players")
abstract fun deleteall()
//GET ALL PLAYERS
@Query("SELECT * FROM players")
abstract fun getAll(): Observable<List<PlayerEntity>>
}
存储库(通过Koin DI使用服务和上面的dao):
class PlayerRepoImpl (
private val remoteDataSource: PlayerService,private val localDataSource: PlayerDao
): PlayerRepo {
override fun fetchAllPlayers(): Observable<List<PlayerUI>> {
return remoteDataSource
.fetchAllPlayers()
.doOnNext {
it.forEach {
Log.d("PlayerRepoImpl",it.username)
}
Log.d("PlayerRepoImpl","Pokusaj upisa u bazu")
val entities = it.map {
PlayerEntity (
it.id,it.username,it.email,it.profileImage,it.universes
)
}
localDataSource.deleteAndInsertAll(entities)
}
.map {
//koristi Resource ovde
it.map {
PlayerUI(it.id,it.universes)
}
}
}
}
viewmodel层:
class Playerviewmodel (
private val playerRepo: PlayerRepo
) : viewmodel(),PlayerContract.viewmodel {
private val subscriptions = Compositedisposable()
override val playerStates: mutablelivedata<PlayerState> = mutablelivedata()
override fun fetchAllPlayers() {
val subscription = playerRepo
.fetchAllPlayers()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
Log.d("Playerviewmodel","Fetched all players from firebase")
it.forEach {
Log.d("Playerviewmodel",it.username)
}
playerStates.value = PlayerState.Success(it)
},{
Log.d("Playerviewmodel","error")
}
)
subscriptions.add(subscription)
}
}
每次我都会收到第二个错误lambda:打印出“ Playerviewmodel”,“错误”。但是如果我注释掉 Repository中的localDataSource.deleteAndInsertAll(entities),它工作正常。 在这个上浪费了很多时间...我很绝望。谢谢。
数据库模型:
@Entity(tableName = "players")
data class PlayerEntity (
@PrimaryKey val id: String,val username: String,val email: String,val profileImage: String,val universes: List<String>
)
API响应模型:
data class PlayerFb (
val id: String,//kopka me da li mi treba
val username: String,//kopka me da li mi treba
val profileImage: String,val universes: List<String>
) {
constructor() : this("","",listof())
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。