Can I ship an android app, with a pre-populated objectbox db in it
Asked Answered
L

1

2

Pretty much as the title says.

I want to launch my Android app, using ObjectBox, but I'd like the data to be prepopulated in the boxes.

Can I package both data.mdb and lock.mdb inside the app, from data I've created during development?

Or is there device specific stuff in there that will need to be generated on each specific device individually.

EDIT -

On checking how big my data.mdb file is it's HUGE!!! I am trying to store a dictionary of words. It has 370k 'rows' each containing a single word, and linking to another box which has a single value in. The file is 171Mb big!! That's outrageous!!!

I have it indexed, and have read that indexes make things large. Is this the case? Is there anything I can do about it?

Lacewing answered 7/8, 2018 at 20:39 Comment(8)
Hi, about the size I need to ask a couple of questions: which ObjectBox are you using? What was the version you initially created the db? Did you try recreating the db with ObjectBox 2.0? How many "words" do you store? What's the average length of words?Subplot
PS.: How many indexes do you have?Subplot
I created it a couple of days ago, using 2.0. I have 370k words. The average length... No idea, it's all the words in the English language! I guess average would be around 6-8? Only have one index on the word itselfLacewing
I stripped out more things to try and make it cleaner and got it down to 112Mb. This is from a text file that is only 4Mb big. I must be doing something wrongLacewing
A dictionary is not the perfect fit to store as individual objects, there are more space efficient ways to store it. Are any properties associated with words?Subplot
Hey, it's ok, I managed to get my original solution working. Just using a plain old HashMap. I turned to ObjectBox because I used it in my last app, and will no doubt use it again in this one. I guess not for a dictionary though :DLacewing
Just curious: how much memory does the HashMap use? You could optimize memory usage if you wanted: pre-sort the strings in your source (txt file?) and put them in a String array. Then do binary search on it.Subplot
I haven't been able to workout how much memory the Hashmap takes up. I'm using Android Studio and the new profiler tool seems to crash AS when I click to take a heap dump!Lacewing
S
3

As to the original question: yes, you can ship your app with an initial data file. You can prepare an ObjectBox database with all the data you need and then grab the data.mdb from the db director (default name is "objectbox").

The BoxStoreBuilder offers the overloaded method initialDbFilethis:

initialDbFile(java.io.File initialDbFile)

initialDbFile(Factory<java.io.InputStream> initialDbFileFactory)

Thus, you can either provide a File or an InputStream.

Subplot answered 9/8, 2018 at 11:9 Comment(2)
what format would it need to be? One of the data.mdb ones that I've already built during development/testing?Lacewing
Yes. Updated the answer accordingly.Subplot

© 2022 - 2024 — McMap. All rights reserved.