在添加数据之前重新调用此行ObjectBox
(如果有更好的解决方案,请纠正我)
boxStore = MyObjectBox.builder().androidContext(this).build();
长答案:
在Application
课堂上
public class BaseApplication extends Application {
public static BaseApplication mInstance;
private BoxStore boxStore;
public static synchronized BaseApplication getInstance() {
return mInstance;
}
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
initializeBoxStore();
}
public BoxStore getBoxStore() {
/* From this method we can get always opened BoxStore */
if (boxStore != null && boxStore.isClosed())
initializeBoxStore();
return boxStore;
}
private void initializeBoxStore() {
boxStore = MyObjectBox.builder().androidContext(this).build();
}
}
在clearAllData
方法
public static void clearAllData(){
BoxStore boxStore = mApp.getBoxStore();
boxStore.close();
boxStore.deleteAllFiles();
}
这解决了我的问题。