Preference Component
The Preference component allows scripts to store and retrieve persistent data. This can be used to save state information, counters, or any small values that need to survive between script executions.
All methods are suspend functions and must be called from a coroutine context (e.g., inside onStart, execute,
or onFinish).
Usage
suspend fun execute(provider: ComponentProvider) {
val preference = provider.preference()
// Store values
preference.setString("apiKey", "abc123xyz")
preference.setBoolean("notificationsEnabled", true)
preference.setInt("retryCount", 3)
preference.setLong("lastRunTimestamp", System.currentTimeMillis())
preference.setFloat("threshold", 0.75f)
// Retrieve values (returns null if the key does not exist)
val apiKey: String? = preference.getString("apiKey")
val enabled: Boolean? = preference.getBoolean("notificationsEnabled")
val retryCount: Int? = preference.getInt("retryCount")
val lastRun: Long? = preference.getLong("lastRunTimestamp")
val threshold: Float? = preference.getFloat("threshold")
// Provide a fallback for missing keys
val count = preference.getInt("retryCount") ?: 5
// Remove a key
preference.delete("lastRunTimestamp")
}
Supported types
| Method pair | Type |
|---|---|
getString / setString |
String |
getBoolean / setBoolean |
Boolean |
getInt / setInt |
Int |
getLong / setLong |
Long |
getFloat / setFloat |
Float |
All getters return a nullable type (null means the key has not been set).
Best Practices
- Use clear and descriptive keys for your preferences
- Always handle the
nullcase when retrieving values — provide a sensible default - Store only small amounts of data; preferences are not designed for large datasets
- Do not store sensitive information such as passwords in plain text