Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
¿Por qué se crean varios almacenes de datos?

java.lang.IllegalStateException: hay varios almacenes de datos activos para el mismo archivo: /data/user/0/com.firstgoalkeeper.firstgoalkeeper/files/datastore/player_pref.preferences_pb. Debe mantener su DataStore como un singleton o confirmar que no hay dos DataStore activos en el mismo archivo (confirmando que el alcance está cancelado).

 class Constants { companion object{ const val PLAYER_PREFERENCE = "player_pref" val PLAYER_SELECTION_KEY = intPreferencesKey("player_selection") } } abstract class PrefsDataStore(context: Context, fileName: String) { private val Context.dataStore: DataStore<Preferences> by preferencesDataStore( fileName ) val mdataStore: DataStore<Preferences> = context.dataStore } class PlayerSelectionDataStore(context: Context) : PrefsDataStore(context, PLAYER_PREFERENCE) { suspend fun storeIndex(index: Int) { mdataStore.edit { it[PLAYER_SELECTION_KEY] = index } } val userSelectionFlow: Flow<Int> = mdataStore.data.map { it[PLAYER_SELECTION_KEY] ?: 4 } } @Composable fun PlayerSelection() { val context = LocalContext.current val playerSelectionDataStore = PlayerSelectionDataStore(context) var index by remember { mutableStateOf(4) } Log.d("index", "PlayerSelection: we are at index ${index} ") Log.d("index", "PlayerSelection: we select ${allTeamsLists[index].name} ") Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier .fillMaxSize() .background(color = goalkeeperBackground) ) { // ... Box( modifier = Modifier .clickable { GlobalScope.launch { playerSelectionDataStore.storeIndex(index) } Toast .makeText( context, "${allTeamsLists[index].name} player is Selected ", Toast.LENGTH_SHORT ) .show() } ... ) {...}

Lo que hice mal y sugiero las mejores prácticas.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

No, estás creando un nuevo objeto PlayerSelectionDataStore en cada recomposición.

Lo menos que puedes hacer es envolverlo con remember :

 val playerSelectionDataStore = remember(context) { PlayerSelectionDataStore(context) }

Una solución más general:

 @Composable fun <T> rememberPreference( key: Preferences.Key<T>, defaultValue: T, ): MutableState<T> { val coroutineScope = rememberCoroutineScope() val context = LocalContext.current val state = remember { context.dataStore.data .map { it[key] ?: defaultValue } }.collectAsState(initial = defaultValue) return remember { object : MutableState<T> { override var value: T get() = state.value set(value) { coroutineScope.launch { context.dataStore.edit { it[key] = value } } } override fun component1() = value override fun component2(): (T) -> Unit = { value = it } } } } private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "preferences")

Uso:

 var text by rememberPreference(stringPreferencesKey("key_text"), defaultValue = "World") TextField(value = text, onValueChange = { text = it })
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda