How to close Objectbox Store and delete data files

落花浮王杯 提交于 2019-12-22 11:36:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!