重新安装应用程序时,旧数据出现在 SQLite 数据库中 [英] Old data appears in SQLite database when reinstalling app

查看:17
本文介绍了重新安装应用程序时,旧数据出现在 SQLite 数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a Xamarin Forms app that uses an SQLite database to store user data, including a login token that I use for authenticating with a REST API.

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

I'm getting a strange issue on Android where updating the app by redeploying it from within Visual Studio causes the data in the SQLite database to revert to an old version (it's always the same old data).

我像这样在 DependencyService 中打开 SQLiteConnection:

I open the SQLiteConnection inside a DependencyService like this:

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;
    }

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

The login token is stored in a table in the database that only ever contains one row (the currently logged in user).

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

Opening the database and reading out this token is literally the first thing I do in App.xaml.cs, after the call to InitializeComponent, and after running an updated Debug or Release build, without fail, the table contains this out of date token. I can then log out and log in again to refresh the token and the app works fine, with the new data persisting correctly over multiple opening and closings of the app.

我的问题是这个过时的令牌来自哪里?

My question is where is this out of date token coming from?

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

Some things I have tried to stop it appearing:

  • 对应用进行了全新构建(包括手动删除项目中的 obj 和 bin 文件夹).
  • 在选项 -> Xamarin -> Visual Studio 中的 Android 设置中取消选中在部署之间在设备上保留应用程序数据缓存"
  • 已从设备上卸载该应用.由于数据库存储在 System.Environment.SpecialFolder.Personal 文件夹中(对应于 /data/user/0/com.example.myapp/files/mydatabase.db3code>),据我所知,这应该会擦除数据库.
  • 更新到最新版本的 Xamarin Forms (2.3.4.231) 和 Xamarin (4.4.0.34).SQLite 库等都是最新的.
  • Did a clean build of the app (including manually deleting the obj and bin folders in the project).
  • Unchecked "Preserve application data cache on device between deploys" in Options -> Xamarin -> Android Settings in Visual Studio
  • Uninstalled the app from the device. Since the data base is stored in the System.Environment.SpecialFolder.Personal folder (which corresponds to /data/user/0/com.example.myapp/files/mydatabase.db3), this should wipe the database as far as I know.
  • Updated to latest version of Xamarin Forms (2.3.4.231) and Xamarin (4.4.0.34). SQLite libs etc are all up to date.

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

There is only one place in my app where I insert the token into the database (when a user logs in) and I am logging this to the console, and the old token is definitely not being reinserted into the database by my app. There is no db3 file in my project that contains this data. In any case how would it get there?

此问题仅在最近几周才开始发生,可能是由于更新所致.在此之前,卸载该应用程序足以清除所有数据.

This issue only began happening in the last couple of weeks, possibly due to an update. Before then, uninstalling the app was sufficient to erase all data.

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

There's clearly some kind of extra layer of caching of this data that I'm not aware of despite searching the SQLite and Xamarin documentation, can anyone tell me what it is?

推荐答案

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

As per the comment by @Commonsware, adding android:allowBackup="false" to the AndroidManifest.xml in the <application> tag prevented the old data from being restored.

来自 Android 文档:

android:allowBackup

是否允许应用程序参与备份和恢复基础架构.如果此属性设置为 false,则不会执行应用程序的备份或恢复,即使是全系统备份,否则会导致所有应用程序数据通过 adb 保存.该属性的默认值为 true.

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.

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

It's still not clear to me why doing a Debug deploy of an app that was currently installed would trigger a restore-from-backup event.

这篇关于重新安装应用程序时,旧数据出现在 SQLite 数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆