【问题标题】:value returned from SQLite using Room Persistence is not being assigned to MutableLiveData使用 Room Persistence 从 SQLite 返回的值未分配给 MutableLiveData
【发布时间】:2020-01-26 12:21:34
【问题描述】:

我想计算表myTable中所有记录的权重字段总和,我试过了:

@Entity(tableName = "myTable")
data class Foo(
    @ColumnInfo(name = "purchaseWeight")
    val weight: Int,
    @ColumnInfo(name = "purchaseDate")
    val date: Long,
    val description: String?

) {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0

}

其他类

data class Sum(val sum: Int)

在道课中

@Dao
interface FooDAO {
    @Query("SELECT SUM(purchaseWeight) AS sum FROM myTable")
    suspend fun calculateSumOfAllWeight(): Sum  
}

在存储库类中

class FooRepository(application: Application) {

    private var fooDAO: FooDAO

    private var sumOfAllWeight = MutableLiveData<Int>()

    init {
        // skipping other code
        CoroutineScope(Dispatchers.IO).launch {
             Log.e(TAG, "Got sum "+ fooDAO.calculateSumOfAllWeight().sum) // Here it prints actual sum of all records
             sumOfAllWeight.postValue(fooDAO.calculateSumOfAllWeight().sum) // but here value is never assigned to MutableLiveData<Int> which is sumOfAllWeight variable
        }
    }

    fun getSumOfAllWeight(): MutableLiveData<Int> {    
        Log.e(TAG, " sumOfAllWeight "+ sumOfAllWeight.value) // it prints null
        return sumOfAllWeight    
    }    
}

ViewModel 类是:

class FooViewModel(
    application: Application
): AndroidViewModel(application) {

    private var repository = FooRepository(application) 

    fun getSumOfAllWeight():MutableLiveData<Int> {
        return repository.getSumOfAllWeight()
    }
}

而 MainActivity.kt 是:

class MainActivity : AppCompatActivity(){

    private lateinit var mFooViewModel: FooViewModel

    private var sumOfAllWeight = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        mFooViewModel = ViewModelProviders.of(this).get(FooViewModel::class.java)

        mFooViewModel.getSumOfAllWeight().observe(
            this,
            Observer {
                sumOfAllWeight = it
            }
        )

        // Set Values to top TextViews
        tv_sum_lbl.text =   "$sumOfAllWeight - KG(s)"  // Here  is set to this view          
    }
}

但它会打印出来

E/SUM_OF_WEIGHT: sumOfAllWeight null
E/SUM_OF_WEIGHT: Got sum 324

无法弄清楚为什么没有将sumOfAllWeight.postValue(fooDAO.calculateSumOfAllWeight().sum) 的值分配给MutableLiveData&lt;Int&gt;

【问题讨论】:

  • 您必须在 Activity/Fragment 中观察 sumOfAllWeight 才能获得更新的值
  • 是的,我在那里观察它也没有显示任何内容
  • 你能添加那些代码吗?如果您检查您的日志顺序,那么您会发现postValue 稍后发生
  • 现在再看看

标签: android android-room kotlin-coroutines android-mvvm


【解决方案1】:

在您的活动内部sumOfAllWeight 是不可观察的。因此,当observer 之外的更改时,它不会反映出来。检查以下代码:

mFooViewModel.getSumOfAllWeight().observe(
    this,
    Observer {
        tv_sum_lbl.text =   "$it - KG(s)" // Now check value here
    }
)

【讨论】:

    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 2018-12-14
    • 2021-08-27
    • 1970-01-01
    • 2022-11-16
    • 2013-10-08
    • 1970-01-01
    • 2015-12-01
    相关资源
    最近更新 更多