Skip to content

Commit

Permalink
调整demo数据发生时机
Browse files Browse the repository at this point in the history
  • Loading branch information
iOrchid committed Mar 24, 2024
1 parent 2f6939f commit ed58d3f
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class JetpackFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.w(TAG, "----- ---- onCreate: 创建Fragment ,可以看得出,每次切换tab,都是新建的fragment")
//模拟生成数据,配合上面vm的获取方式,如果vm是fragment的,就取消注释,如果vm是activity的,就注释下面一行,用activity中的这行代码;
//为的是便于演示效果
vm.startSendScore()
}


Expand Down Expand Up @@ -76,18 +79,15 @@ class JetpackFragment : Fragment() {
//region liveData
private val TAG = "JetpackFragment"
private fun testLiveData() {
//模拟生成数据,配合上面vm的获取方式,如果vm是fragment的,就取消注释,如果vm是activity的,就注释下面一行,用activity中的这行代码;
//为的是便于演示效果
vm.startSendScore()
//观察live的数据变化,不要关联生命周期的lifecycleOwner,observeForever不需要。
//viewLifecycleOwner是fragment的,activity就是自身。
vm.liveScore.observe(viewLifecycleOwner) { str ->
//Fragment观察👀数据
// Log.d(TAG, "Fragment观察👀数据:$str")
Log.d(TAG, "Fragment观察👀数据:$str")
tvLive.text = "文本$str"
}
vm.switchMapLive().observe(viewLifecycleOwner) {
// Log.i(TAG, "Switch Map观察👀数据:$it")
Log.i(TAG, "Switch Map观察👀数据:$it")
tvSwitchLive.text = "数字$it"
}

Expand Down Expand Up @@ -129,7 +129,7 @@ class JetpackFragment : Fragment() {
}

merge.observe(viewLifecycleOwner) {
Log.d(TAG, "MediatorLiveData 观察👀数据:$it")
// Log.d(TAG, "MediatorLiveData 观察👀数据:$it")
}
lifecycleScope.launch {
repeat(10) {
Expand Down

0 comments on commit ed58d3f

Please sign in to comment.