当前位置: 首页 > 知识库问答 >
问题:

Android-LiveData随机不触发更新值(深度问题)

公孙俭
2023-03-14

活动不是问题的一部分。它只是初始化导航图和它的生命周期方法在pause,ondestroy...你可以从视频中推断出(显然,onCreate和onResume被称为点击“smistamento A giro”的瞬间)

在本演示中使用了两个片段,SmistamentoGiroTabFragment(它将子片段作为选项卡,但对于本问题的目的并不重要)和SmistaMentogiroCercagiroFragment。要从一个切换到一个,需要使用两个操作:

  • FindNavController()。导航(SmistaMentogirotabFragmentDirections.ActionDashBoardToCercagiro...
  • FindNavController()。导航(SmistaMentoGiroCartellaCarrelloFragmentDirections.ActionGiroToDashboard...

所以每次我从一个切换到一个时,整个生命周期方法都被调用:

// Removed old fragment I am leaving
SmistamentoGiroCercaGiroFragment - onPause
SmistamentoGiroCercaGiroFragment - onDestroy
// New fragment just added
SmistamentoGiroTabFragment - onAttach
SmistamentoGiroTabFragment - onCreate
SmistamentoGiroTabFragment - onViewCreated
SmistamentoGiroTabFragment - onResume

而另一种方式。

Smistamentogirotabfragment

smistamentoGiroTabViewModel.searchLVCount.observe(viewLifecycleOwner, Observer {
    Logger.debug("TestGrafico", "searchLVCount $it")
})
<TextView
android:id="@+id/oggetti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
app:bindInt="@{viewModel.searchLVCount}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="18" />
val searchLVCount = smistamentoGiroRepository.searchLVCount()
@Query("SELECT COUNT(codiceLdV) FROM SmistamentoGiroLV")
fun searchLVCount(): LiveData<Int>
smistamentoGiroTabViewModel.handleArgs(args, activity().currentView)
return smistamentoGiroLVDao.insert(smistamentoGiroLVEntity)

,在片段观察到的表中插入一条记录:

@Insert(onConflict = OnConflictStrategy.REPLACE)
Long insert(SmistamentoGiroLVEntity element);

您在视频中所观看的测试的对应日志如下:“tabwm init”是SmistamentogirotabviewModel中的以下日志:

init {
        Logger.debug("TestGrafico", "TabWM init")
    }

日志:

// First callback when opening the activity
TestGrafico - TabWM init
TestGrafico - 
TestGrafico - searchLVCount 1

// First automatic insertion
TestGrafico - TabWM init
TestGrafico - 
TestGrafico - searchLVCount 1
TestGrafico - searchLVCount 2

// Second automatic insertion
TestGrafico - TabWM init
TestGrafico - 
TestGrafico - searchLVCount 2
TestGrafico - searchLVCount 3

// Third automatic insertion
TestGrafico - TabWM init
TestGrafico - 
TestGrafico - searchLVCount 3
TestGrafico - searchLVCount 4

// Fourth automatic insertion
TestGrafico - TabWM init
TestGrafico - 
TestGrafico - searchLVCount 4
TestGrafico - searchLVCount 5
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
): View {
    var currentFragment: Fragment? = null
    val binding = FragmentSmistamentoGiroMainBinding.inflate(
            inflater, container, false
    ).apply {
        lifecycleOwner = viewLifecycleOwner
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

private val smistamentoGiroTabViewModel: SmistamentoGiroTabViewModel by viewModels {
    viewModelFactory
}
@Binds
abstract fun bindViewModelFactory(factory: MagazzinoViewModelFactory): ViewModelProvider.Factory

@Binds
@IntoMap
@ViewModelKey(SmistamentoGiroTabViewModel::class)
abstract fun bindSmistamentoGiroTabViewModel(smistamentoGiroTabViewModel: SmistamentoGiroTabViewModel): ViewModel
@MustBeDocumented
@Target(
        AnnotationTarget.FUNCTION,
        AnnotationTarget.PROPERTY_GETTER,
        AnnotationTarget.PROPERTY_SETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
@Singleton
class MagazzinoViewModelFactory @Inject constructor(
        private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        val creator = creators[modelClass] ?: creators.entries.firstOrNull {
            modelClass.isAssignableFrom(it.key)
        }?.value ?: throw IllegalArgumentException("unknown model class $modelClass")
        @Suppress("UNCHECKED_CAST")
        return creator.get() as T
    }
}
TestGrafico - TabWM init SmistamentoGiroTabViewModel@467a7a4
TestGrafico - searchLVCount 18
TestGrafico - searchLVCount 19

TestGrafico - TabWM init SmistamentoGiroTabViewModel@3585336
TestGrafico - searchLVCount 19
TestGrafico - searchLVCount 20

TestGrafico - TabWM init SmistamentoGiroTabViewModel@3351174
TestGrafico - searchLVCount 20
TestGrafico - searchLVCount 21

TestGrafico - TabWM init SmistamentoGiroTabViewModel@c6c785f
// Bug occoured
TestGrafico - searchLVCount 21

由Observer触发的两个值是正确的,因为旧值是fragment/vm初始化,新值是在onCreateView末尾调用的handleArgs方法的结果(导致在db中插入记录),如上面深入解释的。

共有1个答案

苏华藏
2023-03-14

经过整整一个月的测试和分析项目的架构元素,我找到了解决方案。在初始化Room数据库时,我使用了RoomDatabase.JournalMode.Truncate。

下面是模式的ref链接:https://developer.android.com/reference/android/arch/persistence/room/roomdatabase.journalmode

必须使用的是WRITE_AHEAD_LOGGING(默认使用)。通过使用它,每一个随机图形错误都消失了。

 类似资料:
  • 在我的片段中,我观察到db问卷列表字段: 在我的片段中,我有几个按钮,根据哪个按钮被按下,我在viewModel上调用方法,将设置为标签的字符串传递给按钮。 我的视图模式: 存储库方法: Dao方法: 当我按下按钮时,viewModel方法被调用,并带有参数,该参数应该用于通过搜索房间数据库来更新LiveData,但没有任何内容会被无缘无故地更新。数据库不是空的,所以没有理由在主片段中返回null

  • 我试图在下面的代码中找出,为什么当我用新数据填充数据库时,Room的LiveData observable没有给我新的班次。 这是放在我的activity的onCreate方法上的: 这是populateAdapter方法: 我还有以下填充数据库的代码(我使用RxJava在IO线程上完成这项工作,因为Room需要在主线程之外调用它的代码): 当我开始观察ShiftsViewModel之后调用per

  • 我正在尝试更新现有的RealmObject(IncidentCard),其中包含IncidentPhoto类型的RealmList。只要我不尝试更新RealmList,对象就不会出现任何问题,当我包含列表时,我会收到以下错误消息: 这是IncidentCard类: 这是IncidentPhoto类: 要查询域数据库,我创建了这个helper类: 当我添加新的事件卡时,我将此活动称为: 当我捕获新照

  • 问题内容: 我正在寻找一种使用dict update内容更新dict dictionary1的方法 我知道update会删除level2中的值,因为它正在更新最低的密钥level1。 鉴于dictionary1和update可以有任何长度,我该如何解决? 问题答案: @FM的答案具有正确的总体思路,即递归解决方案,但有些特殊的编码和至少一个错误。我建议改为: Python 2: Python 3:

  • 问题内容: 这与其他问题类似,但是在每个数据库的实现中似乎有所不同。我正在使用postgres,似乎不存在类似的东西。 这是我要工作的: 我知道 为什么 不行,但是我似乎找不到适合Postgres的合适解决方案。我希望的值是1到10之间的随机数, 每 行不同。 问题答案: 我认为Postgres可能会优化子查询。这不行吗?

  • 本文向大家介绍Android 事件触发机制的深入学习,包括了Android 事件触发机制的深入学习的使用技巧和注意事项,需要的朋友参考一下  Android 事件触发机制的深入学习 最近在研究android的事件触发和传播机制,说来很惭愧,web下的事件太熟悉不过了,可在android中却很郁闷,常用的触摸事件都糊里糊涂的,在网上看了半天,也整理一份,供大家参考: 监控触摸事件,主要是实现OnGe