Contents

SyncableEntity

An interface that indicates your entity has an identifier that’s consistent across devices.

Declaration

protocol SyncableEntity : AppEntity

Mentioned in

Overview

Adopt the SyncableEntity protocol in your entity types when they have an identifier that’s the same across devices. The presence of this protocol tells the system that it can refer to your entity consistently across devices. For eample, Siri uses this capability to transfer a conversation from one device to another.

If you configure entities with an identifier that’s already consistent across devices, you can adopt this protocol without any additional changes. For example, if you initialize entities with a UUID you retrieve from your server, you can use that value for the identifier and not make any additional changes to your type. The following example shows an app entity type that adopts SyncableEntity and uses a server-based UUID.

struct Article: AppEntity, SyncableEntity {
    var id: UUID  // No changes needed!
    var title: String
}

If your entity maintains different local and stable identifiers, adopt this protocol and set the type of your identifier to SyncableEntityIdentifier. When creating an entity, initialize its id property with both the local and stable identifier values for your type, as shown in the following example. When you need to refer to an entity in your code, use the local identifier.

struct Photo: AppEntity, SyncableEntity {
    var id: SyncableEntityIdentifier<String, String>
    var creationDate: Date

    init(localID: String, stableID: String, creationDate: Date) {
        self.id = SyncableEntityIdentifier(local: localID, stable: stableID)
        self.creationDate = creationDate
    }
}

See Also

App entity types