我正在尝试从 LiveData 迁移到 Room Dao 中的 Flow。应用程序运行良好,但我在测试行为方面遇到问题。当我运行测试时,它无限期地启动和运行。我也尝试使用kotlinx.coroutines.test runBlockingTest,但我遇到了“这项工作尚未完成”的问题,就像这里。有人可以指出我如何测试我的 CoresDao 行为的正确方向吗?
@Dao
interface CoresDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertCores(cores: List<Core>)
@Transaction
suspend fun replaceCoresData(cores: List<Core>) {
deleteAllCores()
insertCores(cores)
}
@Query("SELECT * FROM cores_table")
fun getAllCores(): Flow<List<Core>>
@Query("DELETE FROM cores_table")
suspend fun deleteAllCores()
}
@RunWith(AndroidJUnit4::class)
class CoresDaoTest {
private lateinit var database: SpaceDatabase
private lateinit var coresDao: CoresDao
private val testDispatcher = TestCoroutineDispatcher()
private val testCoresList = listOf(core2, core3, core1)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Before …Run Code Online (Sandbox Code Playgroud) android unit-testing android-room kotlin-coroutines kotlinx.coroutines.flow
我正在使用 Room 数据库进行迁移。我收到此错误:
java.lang.IllegalStateException: Migration didn't properly handle coffee_productivity(io.github.omisie11.coffeeproductivitytracker.database.entity.CoffeeProductivityData).
Expected:
TableInfo{name='coffee_productivity', columns={date=Column{name='date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, productivity=Column{name='productivity', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, coffees_volume=Column{name='coffees_volume', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, number_of_coffees=Column{name='number_of_coffees', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[Index{name='index_coffee_productivity_date', unique=true, columns=[date]}]}
Found:
TableInfo{name='coffee_productivity', columns={date=Column{name='date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, productivity=Column{name='productivity', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, coffees_volume=Column{name='coffees_volume', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, number_of_coffees=Column{name='number_of_coffees', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}}, foreignKeys=[], indices=[Index{name='index_coffee_productivity_date', unique=true, columns=[date]}]}
Run Code Online (Sandbox Code Playgroud)
看起来列的顺序发生了变化,但我所做的唯一一件事就是添加一个新列。实体:
@Entity(tableName = "coffee_productivity", indices = [Index(value = "date", …Run Code Online (Sandbox Code Playgroud) 我的主要应用主题父主题是 MaterialComponents 主题。我更改了颜色,但材质组件(MaterialButton、InputLayout、EditText)没有使用我的强调色(它们使用了一些蓝色,我声明的强调色是蓝绿色)有什么问题?处理主题化材质组件的最佳方法是什么?
我的主题:
<style name="AppTheme" parent="Theme.MaterialComponents">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
编辑:将主题更改为 Theme.MaterialComponents.Bridge 并不能解决该问题。
我正在使用 android 房间来存储我的记录。每条记录包含两个主要字段:日期表示为字符串,当天对应的电话解锁次数为整数。我想知道每次用户解锁手机时在数据库中增加该数字的最佳方法是什么。有没有比查询数据库中当前解锁次数更好的方法,将其增加 1 并更新该字段?有没有一些聪明的方法来增加值而不先进行查询来知道确切的值?