xxfast / KStore

A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and kotlinx.io
https://xxfast.github.io/KStore/
Apache License 2.0
542 stars 18 forks source link

how can I check if a store exists before calling storeOf function #122

Closed SaeedZhiany closed 1 month ago

SaeedZhiany commented 2 months ago

how can I check if a store exists before calling storeOf function? in my application, I want to check whether a store with the given path/key was created before. I don't want to create it if it does not exist.

xxfast commented 2 months ago

KStore is just a delegate to the underlying file of a given path/storage of a given key.

You can use the platform APIs to see if the file exists already / key exists already.

val path =  Path("path/to/myFile")
SystemFileSystem.exists(file) // true if the file exists

Typically we always create the stores and observe it's values to determine if a previously written file/key exists or not

val store = storeOf(file = path)
val cat: Cat? = store.get()
if(cat != null) // previously written value exists 
xxfast commented 1 month ago

Feel free to comment further if you have any more issues.