【发布时间】:2020-12-15 21:55:06
【问题描述】:
最近我正在学习使用 DAO。据我了解,所有@Insert、@Update 或@Query 都是异步执行的。而从纪录片中,@Insert 可以返回一个long 值,这是插入项目的新rowId(如果是多个项目,则为List<long>)。假设我的 DAO 看起来像这样:
@Insert
long insertTransaction(Transaction transaction);
@Insert
List<Long> insertTransactions(List<Transaction> transactions);
当我在 Activity 或 Fragment 中使用这些方法时,是否意味着我在异步任务完成后获得了 long 值?
<!-- language: lang-none -->
// Do I get 0 if the insert is not complete
// or it will wait till the insert is complete and return long?
long id = viewModel.insertTransaction(transaction)
如果它等待异步任务完成,会不会阻塞主线程(尤其是在插入大列表时)?如果没有,如何检查插入是否完成?
【问题讨论】:
标签: android android-room dao android-mvvm