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

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

问题描述

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

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

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

打开数据库并读取此令牌实际上是我在App.xaml.cs中要做的第一件事,在调用InitializeComponent之后,并且在运行更新的Debug或Release版本之后,该表包含了所有内容过期令牌.然后,我可以注销并再次登录以刷新令牌,并且该应用程序可以正常运行,并且新数据可以在该应用程序的多次打开和关闭中正确保存.

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

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

  • 完成了应用的干净构建(包括手动删除项目中的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库等都是最新的.

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

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

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

解决方案

根据@Commonsware的评论,在<application>标记中的AndroidManifest.xml上添加android:allowBackup="false"会阻止恢复旧数据.

来自 Android文档:

android:allowBackup

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

我仍然不清楚为什么对当前安装的应用程序进行Debug部署会触发从备份还原事件.

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.

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).

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).

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:

  • 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.

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.

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?

解决方案

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.

From the 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.

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天全站免登陆