问题
I'v got an exception after I call put()
with my data list.
I know that I closed the box before with a reason (deleteAllFiles() must be closed).
How should I open it again? As I see, dataBoxStore is not null after I closed it.
My code:
dataBoxStore = MyObjectBox.builder().androidContext(this).name("DB").build();
dataBoxStore.close();
dataBoxStore.deleteAllFiles();
final Box<Data> areaLocationBox = dataBoxStore.boxFor(Data.class);
areaLocationBox.put(myList);
Exception:
Caused by: java.lang.IllegalStateException: Store is closed
at io.objectbox.BoxStore.checkOpen(BoxStore.java:261)
at io.objectbox.BoxStore.beginTx(BoxStore.java:310)
at io.objectbox.Box.getWriter(Box.java:107)
at io.objectbox.Box.put(Box.java:389)
回答1:
With ObjectBox 1.3 this got simpler. You can use one of the static BoxStore.deleteAllFiles methods without initiating a BoxStore:
BoxStore.deleteAllFiles(context, "DB");
回答2:
dataBoxStore.put(myList);
Throw the exception of Store is closed while and dataBoxStore is not null.
What I found is to initiate the boxStore again to use it:
dataBoxStore = MyObjectBox.builder().androidContext(this).name("DB").build();
dataBoxStore.close();
dataBoxStore.deleteAllFiles();
dataBoxStore = MyObjectBox.builder().androidContext(this).name("DB").build();
final Box<Data> areaLocationBox = dataBoxStore.boxFor(Data.class);
areaLocationBox.put(myList);
来源:https://stackoverflow.com/questions/47436861/how-to-close-objectbox-store-and-delete-data-files