EntityIdentifier
A type that uniquely identifies a specific instance of an app entity.
Declaration
struct EntityIdentifierOverview
The value used should be unique across all entities of the given type. Entities which are relevant across executions of the application should have stable identifiers that persist across executions.
Entities, by default, conform to the Identifiable protocol. Use a type for the id that conforms to EntityIdentifierConvertible. Default implementations for String, UUID and Int are provided.
For example:
struct Song: AppEntity {
let id = UUID()
}Cross-Device Stable Identifiers
For entities that adopt _SyncableEntity, the framework automatically extracts stable identifiers for cross-device session syncing:
Passthrough case: If your entity’s ID is already stable across devices (like server UUIDs), just adopt
_SyncableEntitywith no other changes. The framework uses your ID as both the local and stable identifier.Mapped case: If your entity has different local and stable identifiers, use
_SyncableEntityIdentifieras your ID type. The framework extracts the stable ID from the wrapper.Custom identifier case: If your entity uses a custom ID type that conforms to
_SyncableEntityIdentifierProviding, the framework extracts the stable ID via the protocol’sstableIdentifierStringproperty.
Apps never interact with EntityIdentifier.stableIdentifier directly - they work with their entity’s ID type (plain types like UUID, _SyncableEntityIdentifier for mapped IDs, or custom types conforming to _SyncableEntityIdentifierProviding).
The stable identifier is NOT used for equality or hashing - two EntityIdentifier instances are equal if they have the same type and local identifier, regardless of stable identifier value.