KStore 0.9.0 Help

Using Platform Specific Paths

Defining Your Own Directories

KStore does not provide a way to create directories for you. You will need to provide the directories where you want to save your files.

var files: Path var cache: Path
val files: KStore<Pet> = storeOf(file = Path("$files/my_cats.json")) val caches: KStore<Pet> = storeOf(file = Path("$cache/my_cats.json"))

On Android

Getting a path on android involves invoking from filesDir/cacheDir from a Context.

import kotlin.io.path.Path class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) files = Path(context.filesDir) cache = Path(context.cacheDir) } }

On Desktop (JVM)

This depends on where you want to save your files, but generally you should save your files in a user data directory. Recommending to use harawata's appdirs to get the platform specific app dir

import java.nio.file.FileSystem const val PACKAGE_NAME = "io.github.xxfast.kstore" const val VERSION = "1.0" const val ORGANISATION = "xxfast" val filesDir = AppDirsFactory.getInstance().getUserDataDir(PACKAGE_NAME, VERSION, ORGANISATION) val cacheDir = AppDirsFactory.getInstance().getUserCacheDir(PACKAGE_NAME, VERSION, ORGANISATION)
import kotlinx.io.files.Path import kotlinx.io.files.SystemFileSystem with(SystemFileSystem) { if(!exists(files)) createDirectories(files) } files = Path(filesDir) cache = Path(cacheDir)

On iOS & other Apple platforms

This depends on where you want to place your files. For most common use-cases, you will want either NSDocumentDirectory or NSCachesDirectory

KStore provides you a convenience extensions to resolve these for you

val fileManager:NSFileManager = NSFileManager.defaultManager val documentsUrl: NSURL = fileManager.URLForDirectory( directory = NSDocumentDirectory, appropriateForURL = null, create = false, inDomain = NSUserDomainMask, error = null )!! val cachesUrl:NSURL = fileManager.URLForDirectory( directory = NSCachesDirectory, appropriateForURL = null, create = false, inDomain = NSUserDomainMask, error = null )!! files = Path(documentsUrl.path) caches = Path(cachesUrl.path)
Last modified: 06 November 2024