欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android dataSotre初次尝试

程序员文章站 2022-07-03 17:01:42
这个库是google最近出的,用来替代sp,今天出差回来尝试一下。1、添加依赖包 // Preferences DataStore implementation "androidx.datastore:datastore-preferences:1.0.0-alpha02"//datastore // Proto DataStore implementation "androidx.datastore:datastore-core:1.0.0-alpha02" i...

这个库是google最近出的,用来替代sp,今天出差回来尝试一下。

1、添加依赖包

 // Preferences DataStore
    implementation "androidx.datastore:datastore-preferences:1.0.0-alpha02"//datastore

    // Proto DataStore
    implementation "androidx.datastore:datastore-core:1.0.0-alpha02"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2"//ktolin协程

2、编写工具类

package com.lhm.datastore.datastore

import android.content.Context
import androidx.datastore.DataStore
import androidx.datastore.preferences.Preferences
import androidx.datastore.preferences.createDataStore
import androidx.datastore.preferences.edit
import androidx.datastore.preferences.preferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

object  DataStoreUtils {
    private val data = preferencesKey<String>("data")
    private val name = "user"
    private var dataStore:DataStore<Preferences>?=null
    /**
     * 写入数据
     */
    suspend fun  writeDataToDataStore(context:Context,msg: String){
        if(dataStore==null) {
            dataStore = context.createDataStore(name = name)
        }
        dataStore!!.edit {
                user->
            user[data] = msg
        }
    }
    /**
     * 读取数据
     */
    fun  readDataToDataStore(context:Context): Flow<String> {
        if(dataStore==null) {
            dataStore = context.createDataStore(name = name)
        }
        return dataStore!!.data.map { preferences: Preferences -> preferences[data] ?: "" }
    }
}

3、进行读写操作

package com.lhm.datastore

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.lhm.datastore.datastore.DataStoreUtils
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect

class MainActivity : AppCompatActivity(),CoroutineScope by MainScope() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        async {
            createData()
        }
        findViewById<TextView>(R.id.tv_getdata).setOnClickListener {
            async {
                getData()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }
    private suspend fun createData() {
        DataStoreUtils.writeDataToDataStore(this,"测试")
    }
    private suspend fun getData(){
        var readDataToDataStore: Flow<String> = DataStoreUtils.readDataToDataStore(this)
        readDataToDataStore.collect{
           Log.e("tag",it)
        }
    }
}

这是简单用法,就先这样吧。

本文地址:https://blog.csdn.net/qq939782569/article/details/109387874

相关标签: android