0

I am currently using objectbox via koin dependency injection within my android app. It works fine however i need to re-initialise my DI and so i need to destroy the boxStore before. This is because i initialise the box via DI and if i do not destroy the current BoxStore I cannot create a new one.

I've found a similar post How to close Objectbox Store and delete data files however it hasn't solved my issue.

I have tried calling deleteAllFiles however i am getting an error.

        BoxStore.deleteAllFiles(context, (BoxStoreBuilder.DEFAULT_NAME))

        BoxStore.deleteAllFiles(context, null)

I am getting the error message:

java.lang.IllegalStateException: Cannot delete files: store is still open

this is on the line of code mentioned above. Any suggestions would be very helpful

nt95
  • 455
  • 1
  • 6
  • 21

3 Answers3

0

solution:

i had to access each of my boxes individually and delete them one at a time.

fun clearAll(){
    firstBox.box.removeAll()
    secondBox.box.removeAll()
    ....... 
}


fun closeAll(){
    firstBox.box.close()
    secondBox.box.close()
    ....... 
}
nt95
  • 455
  • 1
  • 6
  • 21
0

You can just close the boxStore and then delete all files:

boxStore.close();
boxStore.deleteAllFiles();

There's also a static method for deleting all the files ( if you want to delete all files before you open boxStore )

It's the most efficient way.

Ref: https://github.com/objectbox/objectbox-java/issues/317

Kashish Sharma
  • 749
  • 8
  • 18
0

I can suggest a solution. You can get all entity classes and get each box and clear them.

boxStore.getAllEntityClasses().forEach( entityClass -> 
   boxStore.boxFor(entityClass
).removeAll());
commandiron
  • 1,019
  • 1
  • 9
  • 25
kost05
  • 16
  • 1
  • 3