如何解决LiveData变量在另一个LiveData的Observer中不变
在Fragment中,我有一个函数来观察何时将数据插入db
private fun getUsers(interestId: Long) {
homeViewModel.getUsersFromDb(interestId).observe(viewLifecycleOwner,Observer {
it?.let { resource ->
when (resource.status) {
Status.SUCCESS -> {
binding.apply {
progressBar.visibility = View.GONE
}
homeViewModel.setUsers(interestId)
}
Status.ERROR -> {
binding.apply {
progressBar.visibility = View.GONE
Snackbar.make(requireView(),it.message.toString(),Snackbar.LENGTH_SHORT).show()
}
}
Status.LOADING -> {
binding.apply {
progressBar.visibility = View.VISIBLE
}
}
}
}
})
状态为SUCCESS时,应调用homeViewModel.setUsers(interestId)(在我看来这实际上是被调用的),但变量用户不会改变。
class HomeViewModel @Inject constructor(
private val userRepository: UserRepository,private val networkUtil: NetworkUtil,private val sessionManager: SessionManager) : ViewModel() {
var currentUser: UserWithInterests? = null
var users: LiveData<List<UserWithInterests>> = MutableLiveData()
private var count = 0
fun setUsers(interestId: Long) {
// get list from db
users = userRepository.getUsersByInterest(interestId)
}
fun getUsersFromDb(interestId: Long) = liveData(Dispatchers.IO) {
emit(Resource.loading(data = null))
try {
emit(
Resource.success(
data = userRepository.insert(
sessionManager.getUser()!!.id,interestId
)
)
)
} catch (ex: Exception) {
emit(Resource.error(data = null,message = ex.message ?: "Error occurred!"))
}
}
...
}
当我这样改变功能时,一切都正常了。
private fun getUsers(interestId: Long) {
homeViewModel.getUsersFromDb(interestId).observe(viewLifecycleOwner,Observer {
it?.let { resource ->
when (resource.status) {
Status.SUCCESS -> {
binding.apply {
progressBar.visibility = View.GONE
}
}
Status.ERROR -> {
binding.apply {
progressBar.visibility = View.GONE
Snackbar.make(requireView(),Snackbar.LENGTH_SHORT).show()
}
}
Status.LOADING -> {
binding.apply {
progressBar.visibility = View.VISIBLE
}
}
}
}
})
homeViewModel.setUsers(interestId)
@Singleton class UserRepository @Inject constructor(
private val userRetrofitService: UserRetrofitService,private val userDao: UserDao) {
fun getUsersByInterest(interestId: Long) = userDao.getUsersByInterest(interestId)
suspend fun insert(id: Long,interestId: Long) {
val users = userRetrofitService.getUsersByInterest(id,interestId)
userDao.insert(users.map { dtoToEntity(it) })
}
...
}
@Dao abstract class UserDao {
@Transaction
@Query("SELECT * FROM User")
abstract fun getUsers() : LiveData<List<UserWithInterests>>
@Transaction
@Query("SELECT * FROM User u JOIN UserInterest ui ON u.id = ui.userId AND ui.interestId = :interestId")
abstract fun getUsersByInterest(interestId: Long) : LiveData<List<UserWithInterests>>
suspend fun insert(users: List<UserWithInterests>) {
for (u: UserWithInterests in users) {
insertUser(u.user)
u.interests.forEach {
insertUserInterest(UserInterest(u.user.id,it.id))
insertInterest(it)
}
}
}
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract suspend fun insertUser(user: UserEntity)
@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract suspend fun insertUserInterest(userInterest: UserInterest)
@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract suspend fun insertInterest(interest: InterestEntity)
@Delete
abstract suspend fun delete(user: UserEntity)}
我要更改第一个拳头版本才能工作?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。