What’s new with swiftdata? Does it support deduplication with iCloud sync?

Generated on 7/31/2024

1 search

What's New with SwiftData

SwiftData has introduced several new features and improvements:

  1. Schema Macro: A new schema macro allows you to construct compound constraints on persistent models. This includes the unique macro, which ensures that certain combinations of your model's properties remain unique, helping to avoid duplicates in your data. For example, in the Trips app, you can ensure trips are unique across their name, start date, and end date (What’s new in SwiftData).

  2. Custom Data Stores: SwiftData now supports custom data stores, allowing you to use alternative backends like SQLite, remote web services, or even collections of JSON files. This flexibility is achieved through the new datastore protocol (Create a custom data store with SwiftData).

  3. History API: SwiftData history lets your app track modifications to its data, which is useful for features like syncing with a server or responding to changes from an app extension. This includes tracking transactions and changes, making it easier to manage data updates and deletions (Track model changes with SwiftData history).

Deduplication with iCloud Sync

Yes, SwiftData supports deduplication with iCloud sync. The unique macro helps ensure that certain combinations of your model's properties remain unique, which can prevent duplicates when syncing data across devices. When two model instances share the same unique values, SwiftData performs an upsert on collision with an existing model, effectively managing duplicates (What’s new in SwiftData).

Relevant Sessions

  1. Platforms State of the Union
  2. What’s new in SwiftData
  3. Create a custom data store with SwiftData
  4. Track model changes with SwiftData history