4

So I am using ObjectBox with a library called LibGDX, which is a cross platform gaming library written in java. It works by loading what is like a Java applet inside a single Android Activity class, but all the work is done in the applet, and none in Android (apart from instantiating the Activity).

Because of this, my objectBox code is in Java and can't use a context. I therefore run

boxStore = MyObjectBox.builder().androidContext(this).build();

I am running the following code every time I create my DB object in the startup of my app.

private void createMyObjectBox() throws IOException {
    FileHandle fileHandle = Gdx.files.local("objectstorefile");
    if(!fileHandle.isDirectory()) {
        fileHandle.mkdirs();
    }
    store = MyObjectBox.builder().directory(boxStoreDir).build();
}

I get this crash, if I start my app, then hit the home button instantly, then start it again. I think there is a residual MyObjectBox object still in existence, due possibly to the nature of Android and static objects etc, not being fully disposed of.

I'm not sure how I can test for this in order to avoid the error.

Russ Wheeler
  • 2,590
  • 5
  • 30
  • 57

3 Answers3

3

There should always be just a single MyObjectBox instance per directory alive at any time. Thus, you should either close the old store (store.close()), or just keep the store open (e.g. singleton, static, ...). The latter is usually preferred because it is simpler and more efficient.

Update: non-Android code example

static BoxStore store;
public static BoxStore getStore() {
    if(store == null) store = createMyObjectBox();
    return store;
}
Markus Junginger
  • 6,950
  • 31
  • 52
  • 1
    Thanks for the quick reply. I'm struggling to work out how to keep it alive. I've looked at the code for verifyNotAlreadyOpen() and this is where the problem lies. If I open my app, instantly hit the home button then open it again, there is an old version still up, that the 4 lines of System.gc(); System.runFinalization(); System.gc(); System.runFinalization(); can't seem to flush away. Any ideas? – Russ Wheeler Jan 24 '18 at 13:51
  • Should i be closing the store everytime my app shuts down? – Russ Wheeler Jan 24 '18 at 14:10
  • I've run a quick list command on the directory and both times (app runs or app crashes) I have the same two files in the directory. lock.mdb and data.mdb – Russ Wheeler Jan 24 '18 at 14:13
  • Just updated with a simple singleton example. Does that help? – Markus Junginger Jan 24 '18 at 14:19
  • Yes! Well I think so. I hit home, launch app, home launch app a few times and it appeared to fix it....except it did crash once. But Android Studio didn't give me the stack trace! So it may have been totally unrelated. But I consider it fixed now, or at least taken from an edge case to a super edge case :) Thanks again, appreciate the super quick response – Russ Wheeler Jan 24 '18 at 14:30
1

In Kotlin you can make a "home made" singleton like this. This should fix your problem:

companion object{
    var times:Boolean = false
}

object ObjectBox {
    lateinit var boxStore: BoxStore
        private set

       
        fun init(context: Context) {
            if(!times) {
                boxStore = MyObjectBox.builder()
                    .androidContext(context.applicationContext)
                    .build()
                times = true
            }
        }
    }
AntonioC.
  • 11
  • 1
0

You Can use this Way

public class ObjectBox {
    private static  BoxStore boxStore;

    public static  void init(Context context) {

        if (boxStore==null)
        boxStore = MyObjectBox.builder().androidContext(context.getApplicationContext()).build();


    }

    public static BoxStore get() {
        return boxStore;
    }