The Persistence
class encapsulates persistent objects, allowing you to store, retrieve and remove data which will persist across multiple sessions.
Persisted data behaves differently in different testing environments. For consistent results, test persistence in your projects using the Restart function in Meta Spark Player (desktop and mobile) and Meta Spark Studio.
userScope
and StorageScope
have been removed in v157. To update old projects, use Persistence.local
instead.
Your script will need to be slightly different if you are using persistence within Blocks. You can download a sample project here.
Both the main script and the script inside the Block are shown below. The main script is responsible for determining where the Block data is stored:
And the script from the Block:
Property | Description |
---|---|
block | (get) block: StorageLocation Gets the StorageLocation that stores data for this block. This location only returns data that was stored for the currently logged-in user. This storage is specific to this instance of the block: blocks cannot access the data stored by other blocks. Note: The storage is controlled by the parent during blocks initialization. See also BlockStorage . If no BlockStoragewas configured in Blocks.instantiate() via BlockModulesConfig , then the default will be an in-memory, block instance-scopedstorage. |
local | (get) local: StorageLocation Gets an instance of StorageLocation that stores data on the user's local device storage. This location only returns data that was stored for the currently logged-in user. This storage is specific to the given effect: effects cannot access the data stored by other effects. |
userScope | (get) userScope: StorageScope Deprecated. Please use Persistence.local and its StorageLocation class instead. Gets an instance of StorageScope corresponding to the user scope. |
Method | Description |
---|---|
blockStorage | blockStorage(storageIdentifier: string): BlockStorage Represents a BlockStorage for nested Blocks that is scoped the same way as the parent Block's BlockStorage. The storage location (e.g. local or temporary) is kept the same as its parent Block. |
localBlockStorage | localBlockStorage(storageIdentifier: string): BlockStorage Gets the local BlockStorage instance associated with the storageIdentifier . This storageis on the user's device. - calling the API with the same storageIdentifier will return the same BlockStorage object, even if it's completely different Block instances.- Equivalent to Persistence.local for Blocks. |
temporaryBlockStorage | temporaryBlockStorage(storageIdentifier: string): BlockStorage Gets the temporary BlockStorage instance associated with the storageIdentifier . This storagegoes away once the effect restarts or is unloaded, but it does allow for a Block to be unloaded and re-loaded during the lifetime of an effect while maintaining its persistent data. - calling the API with the same storageIdentifier will return the same BlockStorage object, even if it's completely different Block instances. |
Class | Description |
---|---|
BlockStorage | This class represents a storage location for a Block's persistent data. Note it gives no access to the actual data stored within. |
StorageLocation | The StorageLocation class encapsulates different methods of storage for persistent objects. Now with better error handling. |
StorageScope | The StorageScope class encapsulates different methods of storage for persistent objects. |