1

I saw a similar question but it was a decade old:

My objective is to create an app (Android/Kotlin) where the users can search/reference/lookup an individual item from a reference 'book'. Something along the lines of a dictionary or book of recipes.

The intent is for the app to be able to work offline, when the user may be anywhere.

As there could be thousands of items/records to reference it would seem to be too much for an array.

Is a Room database a good option and, if so, can a pre-loaded database be included in the package that is uploaded to Google Play? What is best in 2022?

kotlining
  • 25
  • 1
  • 5
  • You can pre-populate Room DB on app start, check this: https://developer.android.com/training/data-storage/room/prepopulate#from-asset – Darshan Aug 12 '22 at 20:37

1 Answers1

0

Yes, Room will do exactly what you want, but pre-loading it with data will be tricky (but doable).

You can't technically "pre-load" the database, but what you can do is save all the database objects to a file, using either XML, JSON, or any other format. You will then need to save that file in your project as either an Asset or Raw, and on first app launch, you'll need to read that Asset or Raw resource, parse each record, and save it to the database.

Needless to say, that initial process may take some time, so I'm not sure how good that will be from the user's perspective. But it's definitely pretty straight-forward to implement

Creating the Assets folder

Reading files from Assets

user496854
  • 6,461
  • 10
  • 47
  • 84
  • Thanks for the suggestion. I take it that you mean the JSON file would be on the internet and the user would need to wait while it downloaded - after they had installed the app. – kotlining Aug 12 '22 at 03:25
  • No. You would create the JSON file, and include it in your project (as I said, either as an Asset or as Raw). It'll be bundled with your app whenever you build it – user496854 Aug 12 '22 at 03:27
  • OK, and thanks for the links. I'll plan to have a bit of a dabble. – kotlining Aug 12 '22 at 06:03
  • Have got it working and just wanted to note that loading from an exported Room database as an Asset is a quantum faster than loading from a Json file in RAW. So, load the initial data from Json if needed , but then export the database on your development system (phone/emulator) using Android Studio and deploy the app as loading from that exported file now stored as an Asset. – kotlining Aug 17 '22 at 06:56