0

I have been trying out object ox io for Java. It has been working well on Android hence I decided to go with it on Java desktop implementation.

The problem I am facing is that I can save my data at run time but when I restart the app or desktop app the data is gone, is forgotten, which is very strange. I have tried to look for an explanation but haven't found anything on this. Can anyone help or have an idea why this is? In the mean time I have reverted to using SQLite. The app is intended to be very big and have a lot of multi-threads.

here is a sample


import io.objectbox.BoxStore;
import io.objectbox.DebugFlags;
import lombok.SneakyThrows;

import java.io.File;
import java.io.IOException;

public class DBObjectManager {

    public static DBObjectManager  objectIOAccessInstance ;
    private BoxStore store;


    private DBObjectManager() {
        File tempFile = null;
        try {
            tempFile = File.createTempFile("objectdb", "");
        } catch (IOException e) {
            e.printStackTrace();
        }
        tempFile.delete();
         store = MyObjectBox.builder()
                .directory(tempFile)              
                .debugFlags(DebugFlags.LOG_QUERIES | DebugFlags.LOG_QUERY_PARAMETERS)
                .build();
    }

    public static DBObjectManager getinstance() {
        if (objectIOAccessInstance == null) {
            objectIOAccessInstance = new DBObjectManager();
        }
        return objectIOAccessInstance;
    }

    public BoxStore getStore() {
        return store;
    }
}

kleopatra
  • 51,061
  • 28
  • 99
  • 211
kinsley kajiva
  • 1,840
  • 1
  • 21
  • 26

1 Answers1

1

You are creating a new temporary file each time when you execute your application. I guess that explains your problem.

From the docs:

creates a new empty file in the specified directory. deleteOnExit() method is called to delete the file created by this method.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
mipa
  • 10,369
  • 2
  • 16
  • 35
  • yes its working now, so the question is for me, does the app lose access to the files every time we run to make an in-memory type of database every time there is a run, since its temp files are written to disk? – kinsley kajiva May 22 '22 at 18:39
  • 1
    Just use a non-temporary file. – mipa May 23 '22 at 08:37