1

I'm trying to add a version to my database. I followed this answer to do so. Problem is, every time I call GetDefaultSharedPreferences, it doesn't seem to find the version I stored. Here's my code:

public Database() {
    var dbPath = DatabasePath;
    int dbVersion;

    ISharedPreferences sharedPrefs = PreferenceManager.GetDefaultSharedPreferences (Application.Context);
    dbVersion = sharedPrefs.GetInt ("db_ver", 0);

    if (!File.Exists (dbPath) || dbVersion != DATABASE_VERSION) {
        CreateDatabase (dbPath);
    }

    database = new SQLiteConnection (dbPath);
}

void CreateDatabase(string dbPath) {
    var s = Application.Context.Assets.Open (originalDBLocation);
    var writeStream = new FileStream (dbPath, FileMode.OpenOrCreate, FileAccess.Write);
    ReadWriteStream (s, writeStream);
    writeStream.Close ();
    ISharedPreferences sharedPrefs = PreferenceManager.GetDefaultSharedPreferences (Application.Context);
    ISharedPreferencesEditor editor = sharedPrefs.Edit ();
    editor.PutInt ("db_ver", DATABASE_VERSION);
    editor.Commit();
}

When I run the code for the first time after building, dbVersionis always 0. Doesn't matter if I change DATABASE_VERSIONor not. When I close the app on my phone and open it again, the right version is put into dbVersion. So I guess the shared preferences get deleted when the app is reinstalled? This isn't the case in the rest of my app where I use them.

Does ayone know how I can fix this?

Thanks in advance.

Community
  • 1
  • 1
Frederick Eskens
  • 386
  • 3
  • 20

3 Answers3

1

I would suggest, you save the database version inside the database itself. This will avoid conflicts (saved version vs. actual database version) and also keeps those things together.

Considering the unnecessary db calls is good, but there are few points to think about. So any access to settings needs time (db, file or preferences) but with different overhead. But as you need this only once per app lifecycle, it might be ok. Better keep optimising on recurring events and db accesses.

Matt
  • 4,612
  • 1
  • 24
  • 44
  • This will probably work but I'm trying to minimize unnecessary database calls. – Frederick Eskens Apr 26 '16 at 08:49
  • I think, if this call will be only needed once at app start, this might be reasonable. Also it avoids having conflicts (saved version vs. actual database version) – Matt Apr 26 '16 at 08:50
  • 1
    Ok, I'll give it a go. – Frederick Eskens Apr 26 '16 at 08:51
  • Ok, I change my answer a bit so it is less like a comment – Matt Apr 26 '16 at 08:57
  • How can I get the version out of the database, when the database in not yet initialized? – Frederick Eskens Apr 26 '16 at 09:12
  • Maybe I should ask, what are the possible changes for every version? If it's just the layout of the tables, you could specify one table called settings with two string column `Key` and `Value` and `Version` is one entry... This must never change! – Matt Apr 26 '16 at 10:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110292/discussion-between-matt-and-frederickeskens). – Matt Apr 26 '16 at 15:08
0

SharedPreferences are deleted on a re-install.

You could backup the app's SharedPreferences (and other app data) to the user's Google cloud storage.

SharedPreferencesBackupHelper @ https://developer.xamarin.com/api/type/Android.App.Backup.SharedPreferencesBackupHelper/

BackupAgentHelper @https://developer.xamarin.com/api/type/Android.App.Backup.BackupAgentHelper/

Data Backup http://developer.android.com/guide/topics/data/backup.html

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • That's odd. Somewhere else in my code I use `sharedPrefs = GetSharedPreferences("data", FileCreationMode.Private);` and it's stored after re-install. – Frederick Eskens Apr 26 '16 at 08:40
  • @FrederickEskens That is odd as deleting an app will cause the perfs that are associated to the app to be deleted (i.e. the Android OS does not leave dangling associations that would clutter the filesystem.) Now you could just write the version to a text file. – SushiHangover Apr 26 '16 at 09:00
0

You can try this,

public static final String MyPREFERENCES = "MyDBVersion" ;
SharedPreferences sharedpreferences;

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.PutInt ("db_ver", DATABASE_VERSION);
editor.commit();

and when you want to retrieve the same :)

sharedpreferences.getInt("db_ver", 1.0);

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78