0

I am trying to auto-populate a database on first app load. I am trying to 'inflate' the database from a local copy like so:

 public static synchronized bDB getInstance(Context context) {
        if(bDB == null) {
            Log.v("Hello", "Inflating database.");
            bDB = Room.databaseBuilder(context.getApplicationContext(), BDB.class, databaseName)
                    .createFromAsset("database/bdb.db")
                    .fallbackToDestructiveMigration()
                    .build();
        }
        return bDB;
    }

I noticed an error in the initial file (data error, schema remains the same). I corrected it and updated the file 'bdb.db'. However, every time the database gets pre-populated, it always picks up the old, erroneous data. I have tried the following:

  • Cleared and invalidated all caches.
  • Cleaned and rebuilt.
  • Checked the database file a million times.

While the file in the assets folder is correct, where is it picking up the old data from ?
Any help is most welcome.

Sriram
  • 10,298
  • 21
  • 83
  • 136
  • 1
    What version of Android Studio are you using? Bumblebee Canary 5 has issues where it would not actually install your app on API 29+ devices, despite you making changes – ianhanniballake Aug 07 '21 at 22:27
  • 1
    Did you fully uninstall the app before testing the replacement database? Does your app have backups enabled in the manifest? If you use Room 2.3.0, have you tried adding the optional callback parameter to `createFromAsset()` to see if your asset is being used? Have you examined the built APK to confirm that the asset inside the APK has your revised data? – CommonsWare Aug 07 '21 at 22:28
  • I am using Android Studio 4.0. @CommonsWare: I fully uninstalled the app before testing the replacement database. I double checked if any app data remained on the phone. There was none. `createFromAsset()` is already there in the function call. The database inside the APK has the old data. I have set 'allowBackup' to true in the manifest. Do you think that is where the issue is? – Sriram Aug 07 '21 at 22:31
  • @CommonsWare: I think the issue was setting `allowBackup` to true. I made it false and it worked. Could you put that answer and I will mark it correct. Any pointers on what might be going on would be helpful. – Sriram Aug 07 '21 at 22:34
  • "createFromAsset() is already there in the function call" -- yes, I saw. However, I was suggesting adding the optional callback parameter. This is new to Room 2.3.0, and it will let you know if your asset was used. – CommonsWare Aug 07 '21 at 22:41
  • thanks @CommonsWare. Now I understand your point about the callback. Let me take a look. Sounds interesting. – Sriram Aug 07 '21 at 22:43

1 Answers1

1

Based on the discussion in the comments, you seem to have been bitten by auto-backup. With Room, this more typically manifests with a "cannot verify data integrity" error due to schema changes — in your case, the schema was the same, but the data was different.

You can configure auto-backup, both in terms of disabling it (as you did, setting android:allowBackup to false) or controlling what files get backed up (using android:fullBackupContent). And, you can do this on a per-build variant basis, such as having different values for a debug or a release build:

  • For android:allowBackup, you should be able to point to a boolean resource, with different values in main and debug
  • For android:fullBackupContent, you can have different XML resources in main and debug with different rules
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much for your help. Some very helpful pointers there in your answer for me to look further into. – Sriram Aug 09 '21 at 06:55