Assigned
Status Update
Comments
cl...@google.com <cl...@google.com>
qu...@dice.fm <qu...@dice.fm> #2
We don't support cross-process invalidations yet.
ty...@gmail.com <ty...@gmail.com> #3
At least , add a "refresh all observables" function to alpha4 ? i am trying to implement it by myself with your InvalidationTracker class
fr...@gmail.com <fr...@gmail.com> #4
Okey, I made it and created a method to invalidate all observables below;
private void refreshAllLiveData() {
AppDataBase YOUR_DATABASE_WHICH_YOU_BUILD = .....
SupportSQLiteDatabase writableDatabase = YOUR_DATABASE_WHICH_YOU_BUILD.getOpenHelper().getWritableDatabase();
//get the database count;
Cursor cursor = writableDatabase.query("SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name != 'android_metadata' AND name != 'room_master_table';");
int tableCount = 0;
while(cursor.moveToNext()) {
tableCount = cursor.getInt(0);
}
for (int i = 0; i < tableCount; i++) {
//update version table with the incremented count because room modification log stores tables with ids instead of names
writableDatabase.execSQL("INSERT OR REPLACE INTO room_table_modification_log VALUES(null, "+(i)+")");
}
YOUR_DATABASE_WHICH_YOU_BUILD.getInvalidationTracker().refreshVersionsAsync();
}
-----
This is a workaroud for refreshing all live datas, I still prefer to use a proper API you implemented.
Thanx
private void refreshAllLiveData() {
AppDataBase YOUR_DATABASE_WHICH_YOU_BUILD = .....
SupportSQLiteDatabase writableDatabase = YOUR_DATABASE_WHICH_YOU_BUILD.getOpenHelper().getWritableDatabase();
//get the database count;
Cursor cursor = writableDatabase.query("SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name != 'android_metadata' AND name != 'room_master_table';");
int tableCount = 0;
while(cursor.moveToNext()) {
tableCount = cursor.getInt(0);
}
for (int i = 0; i < tableCount; i++) {
//update version table with the incremented count because room modification log stores tables with ids instead of names
writableDatabase.execSQL("INSERT OR REPLACE INTO room_table_modification_log VALUES(null, "+(i)+")");
}
YOUR_DATABASE_WHICH_YOU_BUILD.getInvalidationTracker().refreshVersionsAsync();
}
-----
This is a workaroud for refreshing all live datas, I still prefer to use a proper API you implemented.
Thanx
ka...@gmail.com <ka...@gmail.com> #5
Cross process invalidation is now support in Room 2.1.0 with the enableMultiInstanceInvalidation() API.
ty...@gmail.com <ty...@gmail.com> #6
This works great, thank you!
hu...@verizonconnect.com <hu...@verizonconnect.com> #7
Based on the previous temporary solution suggested here I created this Rule:
class SavedStateHandleRule(
private val route: Any,
) : TestWatcher() {
val savedStateHandleMock: SavedStateHandle = mockk()
override fun starting(description: Description?) {
mockkStatic("androidx.navigation.SavedStateHandleKt")
every { savedStateHandleMock.internalToRoute<Any>(any(), any()) } returns route
super.starting(description)
}
override fun finished(description: Description?) {
unmockkStatic("androidx.navigation.SavedStateHandleKt")
super.finished(description)
}
}
Usage
class ViewModelTest {
private val route = Route("foo")
@get:Rule
val savedStateHandleRule = SavedStateHandleRule(route)
private val viewModel: ViewModel get() = ViewModel(savedStateHandleRule.savedStateHandleMock)
gi...@gmail.com <gi...@gmail.com> #8
Thank you!!
ti...@infraspeak.com <ti...@infraspeak.com> #9
Hello, any updates on this? I'm trying to push typesafety on my code base but having to move everything to instrumented just doest feel right.
cl...@google.com <cl...@google.com> #10
This issue is due to the need to call NavType.put
and NavType.get
internally when handling SavedState. When SavedState was commonized recently in 2.9.0, its Android implementation was delegated to Bundle. So in the end, SavedState in android unit test is still dealing with Bundle. This means there is still no viable solution to completely Android dependency.
However, this does mean that we can make the SavedStateHandle
test api common-friendly.
Description
Component used: Navigation Version used: 2.8.0-beta04 Devices/Android versions reproduced on: All
Navigation arguments are passed to a more details ).
ViewModel
usingSavedStateHandle
(These have traditionally been accessed using a key which returns a String e.g.
savedStateHandle['myId']
. With the new type-safe APIs, however, they are accessed usingsavedStateHandle.toRoute<MyRoute>().myId
which provides type safety.Use of
toRoute
, however, introduces an Android dependency onandroid.os.Bundle
. This means that any tests forViewModel
s which usetoRoute
need to be converted from unit tests to instrumented tests (e.g. using Robolectric) in order to pass.Instrumented tests increase test time and reduce test fidelity. It is preferable for the new type safe APIs to have no Android dependencies.
Steps to repro:
git clone https://github.com/android/nowinandroid/tree/dt/nav-safe-args-android-dependency && cd nowinandroid
./gradlew :feature:topic:testDemoDebugUnitTest --tests "com.google.samples.apps.nowinandroid.feature.topic.TopicViewModelTest" -i
Expected result:
Actual result: