gpt4 book ai didi

android - 重新安装应用程序时旧数据出现在 SQLite 数据库中

转载 作者:太空狗 更新时间:2023-10-29 16:28:58 25 4
gpt4 key购买 nike

我有一个 Xamarin Forms 应用程序,它使用 SQLite 数据库来存储用户数据,包括我用来通过 REST API 进行身份验证的登录 token 。

我在 Android 上遇到一个奇怪的问题,即通过从 Visual Studio 中重新部署来更新应用程序会导致 SQLite 数据库中的数据恢复到旧版本(它始终是相同的旧数据)。

我像这样在 DependencyService 中打开 SQLiteConnection:

public SQLite.Net.SQLiteConnection GetConnection()
{
var sqliteFilename = "mydatabase.db3";
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder
var path = Path.Combine(documentsPath, sqliteFilename);

var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
var conn = new SQLite.Net.SQLiteConnection(platform, path);

// Return the database connection
return conn;
}

登录 token 存储在数据库的一个表中,该表只包含一行(当前登录的用户)。

打开数据库并读出这个 token 实际上是我在 App.xaml.cs 中做的第一件事,在调用 InitializeComponent 之后,并且在运行更新的调试或发布构建之后,没有失败,该表包含此过时的标记。然后我可以注销并再次登录以刷新 token ,应用程序运行正常,新数据在多次打开和关闭应用程序时正确保留。

我的问题是这个过时的 token 是从哪里来的?

我试图阻止它出现的一些事情:

  • 干净地构建应用(包括手动删除项目中的 obj 和 bin 文件夹)。
  • 在选项 -> Xamarin -> Visual Studio 中的 Android 设置中取消选中“在部署之间在设备上保留应用程序数据缓存”
  • 已从设备上卸载该应用。由于数据库存储在 System.Environment.SpecialFolder.Personal 文件夹中(对应于 /data/user/0/com.example.myapp/files/mydatabase.db3),据我所知,这应该会删除数据库。
  • 已更新至最新版本的 Xamarin Forms (2.3.4.231) 和 Xamarin (4.4.0.34)。 SQLite 库等都是最新的。

我的应用程序中只有一个地方可以将 token 插入数据库(当用户登录时),我将其记录到控制台,我的应用程序绝对不会将旧 token 重新插入数据库.我的项目中没有包含此数据的 db3 文件。无论如何,它会如何到达那里?

这个问题最近几周才开始出现,可能是由于更新。在此之前,卸载该应用程序足以清除所有数据。

显然,尽管搜索了 SQLite 和 Xamarin 文档,但我并不知道这些数据有某种额外的缓存层,谁能告诉我它是什么?

最佳答案

根据@Commonsware 的评论,添加 android:allowBackup="false"AndroidManifest.xml<application>标签阻止了旧数据的恢复。

来自Android docs :

android:allowBackup

Whether to allow the application to participate in the backup and restore infrastructure. If this attribute is set to false, no backup or restore of the application will ever be performed, even by a full-system backup that would otherwise cause all application data to be saved via adb. The default value of this attribute is true.

我仍然不清楚为什么对当前安装的应用进行调试部署会触发从备份恢复事件。

关于android - 重新安装应用程序时旧数据出现在 SQLite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711590/

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