Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Old data appears in SQLite database when reinstalling app

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?

like image 377
samgak Avatar asked Dec 19 '22 07:12

samgak


1 Answers

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.

like image 56
samgak Avatar answered Feb 24 '23 19:02

samgak