क्यों वहाँ कई डेटास्टोर बनाया जाता है?

0

सवाल

जावा.लैंग.IllegalStateException: वहाँ रहे हैं कई DataStores एक्टिव इस के लिए एक ही फ़ाइल: /डेटा/उपयोगकर्ता/0/com.firstgoalkeeper.firstgoalkeeper/फ़ाइलें/डेटास्टोर/player_pref.preferences_pb. आप चाहिए या तो बनाए रखने के अपने डेटास्टोर के रूप में एक सिंगलटन या की पुष्टि करें कि वहाँ है कोई दो डेटास्टोर पर सक्रिय है एक ही फ़ाइल (द्वारा पुष्टि है कि गुंजाइश रद्द कर दिया है).

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()
                }
                ...
        ) {...}

क्या मैं गलत था और सुझाव है कि सबसे अच्छा अभ्यास है.

1

सबसे अच्छा जवाब

1

सही नहीं कर रहे हैं बनाने के एक नए PlayerSelectionDataStore वस्तु पर प्रत्येक recomposition.

कम से कम आप कर सकते हैं के साथ यह लपेटकर remember:

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

एक अधिक सामान्य समाधान:

@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")

उपयोग:

var text by rememberPreference(stringPreferencesKey("key_text"), defaultValue = "World")
TextField(value = text, onValueChange = { text = it })
2021-11-24 06:46:00

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में