小编ko2*_*2ic的帖子

Why does a call to a logic method on a ViewModel with StateFlow as a property cause a recomposition even though the value has not changed?

This application counts up A when C is pressed. Since A is the only one that has changed, I expected the recomposition to be just that. But C is also recomposition.

Here is the code.

ViewModel exposes StateFlow.

class MainViewModel : ViewModel() {
  private val _count: MutableStateFlow<Int> = MutableStateFlow(0)
  val count: StateFlow<Int> = _count.asStateFlow()
  fun increaseCount() {
    _count.value++
  }
}
Run Code Online (Sandbox Code Playgroud)

CCompose calls increaseCount().

@Composable
fun CountUpScreen(
  modifier: Modifier = Modifier,
  viewModel: MainViewModel = viewModel(),
) {
  val count: Int …
Run Code Online (Sandbox Code Playgroud)

android android-jetpack-compose compose-recomposition

2
推荐指数
1
解决办法
612
查看次数

“ kotlin-noarg”插件在Realm中不起作用

“ kotlin-allopen”插件有效,但“ kotlin-noarg”插件无效。我能怎么做?

下面是代码。

build.gradle

buildscript {
    ext.kotlin_version = '1.1.3-2'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:3.5.0"
    }
}
apply plugin: "kotlin-allopen"
apply plugin: "kotlin-noarg"

allOpen {
    annotation("sample.AllOpen")
}
noArg {
    annotation("sample.NoArg")
    invokeInitializers = true
}
Run Code Online (Sandbox Code Playgroud)

app / build.gradle

apply plugin: 'realm-android'
Run Code Online (Sandbox Code Playgroud)

NoArg.kt

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class NoArg
Run Code Online (Sandbox Code Playgroud)

MyApplication.kt

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Realm.init(this)
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml

<application
    android:name=".MyApplication"
Run Code Online (Sandbox Code Playgroud)

SampleEntity.kt

@NoArg
@AllOpen
@RealmClass
data …
Run Code Online (Sandbox Code Playgroud)

android realm kotlin

1
推荐指数
1
解决办法
1059
查看次数