Storage¶
Key/value persistence backed by the platform-native store
(NSUserDefaults on iOS, SharedPreferences on Android, a local
JSON file in desktop tests). All operations are coroutines so they
don't block the framework loop.
Cross-platform key/value persistence (AsyncStorage).
Mirrors React Native's AsyncStorage API but with native async
coroutines and a JSON convenience layer. Values are persisted using
the platform-appropriate key/value store:
- iOS:
NSUserDefaults(standard user defaults). - Android:
SharedPreferences(file"pn_async_storage"). - Desktop / tests: an in-memory dict optionally backed by a JSON
file under
FileSystem.app_dirfor inter-run persistence during local development.
All operations are coroutines so they don't block the framework loop;
the underlying native calls are dispatched via
:func:asyncio.to_thread.
Example
Classes:
| Name | Description |
|---|---|
AsyncStorage |
Async key/value persistence layered on platform-native stores. |
Functions:
| Name | Description |
|---|---|
use_persisted_state |
Persisted |
AsyncStorage
¶
Async key/value persistence layered on platform-native stores.
Every method is a coroutine that returns when the underlying
native operation completes. Strings round-trip directly via
get /
set; richer values can
use get_json /
set_json which
add a JSON encode/decode step.
Methods:
| Name | Description |
|---|---|
get |
Return the string stored at |
set |
Persist |
delete |
Remove the entry at |
all_keys |
Return every persisted key (order is platform-dependent). |
clear |
Remove every entry written through |
get_json |
Return the JSON-decoded value stored at |
set_json |
JSON-encode |
get
async
staticmethod
¶
Return the string stored at key, or None if missing.
set
async
staticmethod
¶
Persist value under key.
value must be a str. For non-string values, use
set_json.
delete
async
staticmethod
¶
delete(key: str) -> None
Remove the entry at key if present (no-op otherwise).
all_keys
async
staticmethod
¶
Return every persisted key (order is platform-dependent).
get_json
async
staticmethod
¶
Return the JSON-decoded value stored at key, or None.
If the stored value isn't valid JSON, returns None rather
than raising; assume the entry was written by another
process or an older version of the app.
use_persisted_state
¶
Persisted use_state variant.
Backed by AsyncStorage:
behaves like use_state but loads the prior value (if any) on
mount and persists every subsequent update. Until the load
completes the value is initial, the same fallback React
Native users get with AsyncStorage.getItem.
The setter accepts either a value or a current -> new
callable, matching
use_state. Writes are
fire-and-forget; failures are silently absorbed (storage is
best-effort by design).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Storage key. Pass a stable, namespaced string
(e.g. |
required |
initial
|
T
|
Value used before the first load completes. |
required |
Returns:
| Type | Description |
|---|---|
T
|
|
Callable[[Any], None]
|
Patterns¶
- Token / session storage: write strings with
setand read withget. - Complex values: use
set_json/get_jsonfor dicts, lists, and primitives. - Component state that survives restarts: reach for
use_persisted_stateinstead of manually wiring an effect toAsyncStorage.
Next steps¶
- The pattern is walked through end-to-end in the Async + data guide.