Reputation: 1574
I am using ObjectBox
version 1.5.0
Here is the method to delete all the data from the database.
public static void clearAllData(){
mApp.getBoxStore().close();
mApp.getBoxStore().deleteAllFiles();
}
I want to delete all the data from ObjectBox when user press on Logout button. Above method is deleting all the data but again when I want to add data I'm getting
IllegalStateException: Store is closed
Problem: How to re-open closed BoxStore
?
Upvotes: 0
Views: 842
Reputation: 1574
Re-call this line before adding data in ObjectBox
(Correct me if there is better solution )
boxStore = MyObjectBox.builder().androidContext(this).build();
Long Answer:
In the Application
Class
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();
}
}
In clearAllData
Method
public static void clearAllData(){
BoxStore boxStore = mApp.getBoxStore();
boxStore.close();
boxStore.deleteAllFiles();
}
This solved my problem.
Upvotes: 1