再抱一抱DataStore

简介: DataStore 中使用到了 Flow 、协程,在使用的时候更适合现在的写法,但不能只因为他的写法简单就使用啊,目前即使DataStore 已经发布了 beta 版本,但是性能还是堪忧

一片爆红

在之前我发过一篇 DataStore 的文章:用力抱一下 Jetpack DataStore,当时使用的版本是1.0.0-alpha05

20210524100401171.png

大家也都知道 Google 的性格,在 alpha 版本的库的 API 随时可能修改,事实证明我的猜测是正确的,在更新了 DataStore 的版本之后果然一片爆红。

* init Context 
*@ param context Context 
 fun init ( context : Context ){
 dataStore = context . createDataStore ( preferenceName )
}
 fun readIntFlow ( key : String , default : Int =0): Flow < Int >=
 dataStore . data 
. Catch {
 this : FlowCollector < Preferences >
 if ( it is IoException ){
 it . printStackTrace ()
 emit ( emptyPreferences ())} else {
 throw it 
}. map { it : Preferences 
 it [ preferencesKey ( key )]?: default 
}
 fun readIntData ( key : String , default : Int =0): Int {
 var yalue =0
 runBlocking { this : CoroutineScope 
 dataStore . data . first { it : Preferences 
 value = it [ preferencesKey ( key )]?: default 
 true 
 first 
}
}
 return value 

但不要担心,发布了 beta 版本就证明 API 已经基本完善了,也就是说之后 API 就不会有大的改动了,剩下的就是性能方面的优化了。

开始使用

之前为了使用 DataStore 我还专门写了一个工具类,但现在看来是多余的了,来看看如何使用 beta 版本的 DataStore 吧。

添加依赖

第一步肯定要来添加下依赖。

// DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-beta01"

初始化 DataStore

在之前 alpha 版本中,初始化方法如下:

context.createDataStore(preferenceName)

通过 Context 的扩展方法 createDataStore 来创建一个 DataStore ,但现在不可以了,这个方法已经被删除,那么现在该如何创建 DataStore 呢?现在应该使用由 preferencesDataStore 创建的属性委托来创建 Datastore<Preferences> 实例。

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "PlayAndroidDataStore")

在项目中只在顶层调用一次 preferencesDataStore 方法,便可在应用的所有其余部分通过此属性访问该实例,这样可以更轻松地将 DataStore 保留为单例。

下面咱们来看看 preferencesDataStore 的源码吧。

public fun preferencesDataStore(
    name: String,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    produceMigrations: (Context) -> List<DataMigration<Preferences>> = { listOf() },
    scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
): ReadOnlyProperty<Context, DataStore<Preferences>> {
    return PreferenceDataStoreSingletonDelegate(name, corruptionHandler, produceMigrations, scope)
}

可以看到 preferencesDataStore 有四个参数,其中只有名字是必选参数,剩下的参数都有对应的默认值,下面就来看看这几个参数的含义吧。

  • name: DataStore 的名字,不多说;
  • corruptionHandler:如果DataStore在尝试读取数据时遇到CorruptionException,则将调用destroyHandler。无法对数据进行反序列化时,序列化程序会引发CorruptionException;
  • produceMigrations:产生迁移。 ApplicationContext作为参数传递给这些回调。在对数据进行任何访问之前,都要运行DataMigrations。不管是否成功,每个生产者和迁移都可以运行一次以上(可能是因为另一个迁移失败或对磁盘的写入失败)。
  • scope:作用域

上面介绍了 preferencesDataStore 方法中的参数,大家可以根据需求自行填写使用。

使用DataStore

创建 DataStore

使用的时候首先需要调用下咱们刚定义的 Context 的扩展属性:dataStore。

private lateinit var dataStore: DataStore<Preferences>
/**
 * init Context
 * @param context Context
 */
fun init(context: Context) {
    dataStore = context.dataStore
}

之后就可以通过dataStore进行操作了。

保存数据

先来看看如何进行数据的保存。

suspend fun saveBooleanData(key: String, value: Boolean) {
    dataStore.edit { mutablePreferences ->
        mutablePreferences[booleanPreferencesKey(key)] = value
    }
}
suspend fun saveIntData(key: String, value: Int) {
    dataStore.edit { mutablePreferences ->
        mutablePreferences[intPreferencesKey(key)] = value
    }
}
suspend fun saveStringData(key: String, value: String) {
    dataStore.edit { mutablePreferences ->
        mutablePreferences[stringPreferencesKey(key)] = value
    }
}
suspend fun saveFloatData(key: String, value: Float) {
    dataStore.edit { mutablePreferences ->
        mutablePreferences[floatPreferencesKey(key)] = value
    }
}
suspend fun saveLongData(key: String, value: Long) {
    dataStore.edit { mutablePreferences ->
        mutablePreferences[longPreferencesKey(key)] = value
    }
}

大家在使用的时候可以根据不同类型来分别进行保存,当然了,如果你不想分的这么细,也可以写一个方法来进行统一调用。

suspend fun <U> putData(key: String, value: U) {
    when (value) {
        is Long -> saveLongData(key, value)
        is String -> saveStringData(key, value)
        is Int -> saveIntData(key, value)
        is Boolean -> saveBooleanData(key, value)
        is Float -> saveFloatData(key, value)
        else -> throw IllegalArgumentException("This type can be saved into DataStore")
    }
}

读取数据

保存完数据就该进行读取了,来看看如何读取数据吧。

fun readBooleanFlow(key: String, default: Boolean = false): Flow<Boolean> =
    dataStore.data
        .catch {
            //当读取数据遇到错误时,如果是 `IOException` 异常,发送一个 emptyPreferences 来重新使用
            //但是如果是其他的异常,最好将它抛出去,不要隐藏问题
            if (it is IOException) {
                it.printStackTrace()
                emit(emptyPreferences())
            } else {
                throw it
            }
        }.map {
            it[booleanPreferencesKey(key)] ?: default
        }
fun readIntFlow(key: String, default: Int = 0): Flow<Int> =
    dataStore.data
        .catch {
            if (it is IOException) {
                it.printStackTrace()
                emit(emptyPreferences())
            } else {
                throw it
            }
        }.map {
            it[intPreferencesKey(key)] ?: default
        }
fun readStringFlow(key: String, default: String = ""): Flow<String> =
    dataStore.data
        .catch {
            if (it is IOException) {
                it.printStackTrace()
                emit(emptyPreferences())
            } else {
                throw it
            }
        }.map {
            it[stringPreferencesKey(key)] ?: default
        }
fun readFloatFlow(key: String, default: Float = 0f): Flow<Float> =
    dataStore.data
        .catch {
            if (it is IOException) {
                it.printStackTrace()
                emit(emptyPreferences())
            } else {
                throw it
            }
        }.map {
            it[floatPreferencesKey(key)] ?: default
        }
fun readLongFlow(key: String, default: Long = 0L): Flow<Long> =
    dataStore.data
        .catch {
            if (it is IOException) {
                it.printStackTrace()
                emit(emptyPreferences())
            } else {
                throw it
            }
        }.map {
            it[longPreferencesKey(key)] ?: default
        }

同样的,根据类型不同选择不同的读取方法进行使用即可,当然了,也可以像上面保存数据一样再写一个方法进行统一调用也可以。

fun <U> getData(key: String, default: U): Flow<U> {
    val data = when (default) {
        is Long -> readLongFlow(key, default)
        is String -> readStringFlow(key, default)
        is Int -> readIntFlow(key, default)
        is Boolean -> readBooleanFlow(key, default)
        is Float -> readFloatFlow(key, default)
        else -> throw IllegalArgumentException("This type can be saved into DataStore")
    }
    return data as Flow<U>
}

进阶使用

上面已经描述了如何简单使用 DataStore ,但只能使用一些基础类型,如果咱们想要使用数据类呢?当然可以使用 DataStore 所支持的 Proto,但这里咱们不说这个,应为在 Kotlin 中也可以直接通过数据类序列化的方式来使用 DataStore ,来看看如何使用吧。

定义数据类

首先咱们来定义一个数据类吧。

data class ZhuPreferences(
    val name: String,
    val age: Int
)

数据类很简单,只有两个参数,名字和年龄。

实现 DataStore 序列化器

下一步需要做的是来实现 DataStore 的序列化器。

@Serializable
data class ZhuPreferences(
    val name: String = "jiang",
    val age: Int = 20
)
object ZhuPreferencesSerializer : Serializer<ZhuPreferences> {
    override val defaultValue = ZhuPreferences()
    override suspend fun readFrom(input: InputStream): ZhuPreferences {
        try {
            return Json.decodeFromString(
                ZhuPreferences.serializer(), input.readBytes().decodeToString()
            )
        } catch (serialization: SerializationException) {
            throw CorruptionException("Unable to read UserPrefs", serialization)
        }
    }
    override suspend fun writeTo(t: ZhuPreferences, output: OutputStream) {
        output.write(Json.encodeToString(ZhuPreferences.serializer(), t).encodeToByteArray())
    }
}

由于 Parcelables 与 DataStore 一起使用并不安全,因为不同 Android 版本之间的数据格式可能会有所变化,所以这里使用 Serializable 的方式进行序列化。

使用 DataStore 序列化器

创建 DataStore

数据类都准备好了,下面来创建 DataStore ,这里不能使用上面提到的 preferencesDataStore 方法来进行创建了,而是需要通过 dataStore 方法。

val Context.dataStores by dataStore("test", serializer = ZhuPreferencesSerializer)

写入数据

来看看如何写入数据类的数据吧。

private suspend fun setDataStore(zhuPreferences: ZhuPreferences) {
    dataStores.updateData {
        it.copy(name = zhuPreferences.name, age = zhuPreferences.age)
    }
}

从上面代码中可以看到通过 dataStores 中的 updateData 方法来进行修改数据,然后再使用生成的 .copy() 函数更新数据。

读取数据

写入完成之后来看看如何进行读取数据吧。

private suspend fun getDataStore() {
    val name = dataStores.data.first().name
    val age = dataStores.data.first().age
}

由于在创建 dataStores 的时候已经传入了序列化器,所以读取数据很简单,直接可以调取数据类中的参数进行使用。

总结

DataStore 中使用到了 Flow 、协程,在使用的时候更适合现在的写法,但不能只因为他的写法简单就使用啊,目前即使DataStore 已经发布了 beta 版本,但是性能还是堪忧,但我相信 DataStore 在正式版本发布的时候性能应该会追上甚至超越 MMKV 和 SP 。



目录
相关文章
|
XML 存储 JSON
Android Jetpack组件 DataStore的使用和简单封装
Android Jetpack组件 DataStore的使用和简单封装
2137 0
Android Jetpack组件 DataStore的使用和简单封装
|
SQL 自然语言处理 BI
另辟蹊径的 Text2SQL,不用大模型也能搞 chatBI
润乾报表NLQ组件摒弃大模型路线,采用规则词典与领域知识库,将自然语言精准转化为MQL查询语言,实现稳定、低成本、可维护的ChatBI。其核心在于结构化语义解析,避免“幻觉”,支持复杂多表关联与计算,适用于企业级BI场景,是可靠高效的自然语言查询解决方案。
|
人工智能 供应链 数据可视化
数字孪生智慧园区管理平台,三维可视化系统,沃思智能
智慧园区加速发展,2025年全球市场规模将超3000亿美元。依托物联网、AI等技术,管理系统实现安防、能源、空间等全链条智能化,推动园区从“物业服务”向“数据运营”转型,助力产城融合与绿色发展。(238字)
1308 138
|
机器学习/深度学习 数据可视化 算法
深度学习模型结构复杂、参数众多,如何更直观地深入理解你的模型?
深度学习模型虽应用广泛,但其“黑箱”特性导致可解释性不足,尤其在金融、医疗等敏感领域,模型决策逻辑的透明性至关重要。本文聚焦深度学习可解释性中的可视化分析,介绍模型结构、特征、参数及输入激活的可视化方法,帮助理解模型行为、提升透明度,并推动其在关键领域的安全应用。
1198 0
|
数据采集 存储 前端开发
如何开发门店业绩上报管理系统中的销售日报板块?(附架构图+流程图+代码参考)
门店业绩上报管理系统中的销售日报板块是零售企业管理运营的核心工具。通过汇总当日销售数据,系统帮助管理者实时掌握门店经营状况,提升决策效率和市场竞争力。本文详解销售日报模块的功能设计、业务流程、开发技巧及实现效果,涵盖数据库设计、前端展示、数据采集与分析等内容,助力企业打造高效精准的销售日报系统,实现数据驱动的精细化管理。(239字)
|
前端开发
Promise.all()方法的作用是什么?
Promise.all()方法的作用是什么?
927 121
|
11月前
|
图形学
游戏外包开发的注意事项
游戏外包常因信息差、标准不一导致“货不对板”或“进度失控”。为确保项目落地,需严控需求精准度、明确知识产权、统一技术标准、分阶段验收付款,并保持沟通透明。警惕低价陷阱与源码隐瞒,建议根据实际阶段制定风控策略。#游戏外包 #游戏开发
|
12月前
|
存储 弹性计算 缓存
阿里云服务器热门实例适用场景:e/u1/u2i/u2a/c9a/g9a/r9a实例适用场景汇总
通过阿里云目前的活动购买云服务器时,除了轻量应用服务器之外,活动内的云服务器实例规格主要以e/u1/u2i/u2a/c9a/g9a/r9a这几种为主。e指的是经济型e实例,u1/u2i/u2a是指通用算力型实例,c9a/g9a/r9a均属于最新第9代AMD企业级实例,不同实例规格的云服务器价格不一样,适用场景也不一样。本文主要为大家详细介绍了阿里云服务器的实例规格是什么,有什么用,并汇总了轻量应用服务器和云服务器e/u1/u2i/u2a/c9a/g9a/r9a实例规格的适用场景,以供参考和选择适合自己的需求的实例规格。
|
12月前
|
架构师 Java 数据库
Java开发进阶:从初级工程师到架构师的能力提升路径
Java开发者从初级到架构师需经历技术与软实力的全面提升。本文梳理各阶段能力要求:夯实基础、掌握主流框架、深入分布式技术、培养系统设计与业务洞察力,助力开发者明确职业进阶路径,成长为具备全局视野的技术领导者。