gpt4 book ai didi

Android 数据存储区 IOException 无法重命名为

转载 作者:行者123 更新时间:2023-12-04 03:47:47 26 4
gpt4 key购买 nike

我正在尝试在我的项目中实现 Jetpack Datastore。我正在使用 apha-01版本并且代码运行良好。然后我在 Gradle 文件中看到有一个新版本,所以我将其更新为 alpha-03 .
启动我的应用程序后,我遇到了另一个问题。我发现在alpha-03中没有找到Proto库版本所以我回滚到版本 alpha-01 .另外,我试过 alpha-02 .从那以后,我遇到了以下错误:

 Process: com.montymobile.sands, PID: 19928
java.io.IOException: /data/user/0/com.montymobile.sands/files/datastore/sns_preferences.preferences_pb.tmp could not be renamed to /data/user/0/com.montymobile.sands/files/datastore/sns_preferences.preferences_pb
at androidx.datastore.SingleProcessDataStore.writeData$datastore_core_release(SingleProcessDataStore.kt:304)
at androidx.datastore.SingleProcessDataStore.transformAndWrite(SingleProcessDataStore.kt:282)
at androidx.datastore.SingleProcessDataStore$actor$1.invokeSuspend(SingleProcessDataStore.kt:165)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
我注意到发生这种情况是因为我一个接一个地保存了两个不同的 key ,每个都在一个协程中。当我评论第二个 Action 时,它起作用了。
谁能解释为什么会发生这种情况?以及如何保存尽可能多的值?
任何帮助,将不胜感激。

最佳答案

Screenshot of the sime UI 检查以下代码。

class DataStoreClass(private val context: Context) {

private fun dataStore(): DataStore<Preferences> = context.createDataStore("settings")

suspend fun <T> save(key: Preferences.Key<T>, value: T) {
dataStore().edit {
it[key] = value
}
}

suspend fun <T> read(key: Preferences.Key<T>, defaultValue: T): T {
val pre = dataStore().data.first()
return pre[key] ?: defaultValue
}

}

open class BaseActivity : AppCompatActivity() {

inline fun <reified T> getSavedValue(key: String, defaultValue:T): Any {
var savedValue = Any()
runBlocking {
val operation = async {
val savedStr = DataStoreClass(this@BaseActivity).read(preferencesKey(key),
defaultValue!!).let {
it
}
savedValue = savedStr
}
operation.await()
}
return savedValue
}

fun <T> saveDesiredValue(key: Preferences.Key<T>, value: T) {
runBlocking {
CoroutineScope(Dispatchers.IO).launch {
DataStoreClass(this@BaseActivity).save(key, value)
}.join()
}
}
}

class MainActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val bnt = findViewById<Button>(R.id.btnSave)
val btnRead = findViewById<Button>(R.id.btnRead)
val key = findViewById<EditText>(R.id.key)
val etValue = findViewById<EditText>(R.id.etValue)
val value = findViewById<TextView>(R.id.value)

bnt.setOnClickListener {
saveDesiredValue(preferencesKey(key.text.toString()),etValue.text.toString())
saveDesiredValue(preferencesKey("test"), Random.nextInt(100))

}

btnRead.setOnClickListener {
value.text = "${getSavedValue(key.text.toString(),"")} " +
"${getSavedValue("test",0)}"
}
}
}

关于Android 数据存储区 IOException 无法重命名为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64883210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com