Skip to main content

How to check whether a SwiftData model object has been deleted

About 1 minSwiftArticle(s)bloghackingwithswift.comcrashcourseswiftswiftdataxcodeappstore

How to check whether a SwiftData model object has been deleted 관련

SwiftData by Example

Back to Home

How to check whether a SwiftData model object has been deleted | SwiftData by Example

How to check whether a SwiftData model object has been deleted

Updated for Xcode 15

SwiftData can get confused if you accidentally attempt to insert the same object instance twice, which can sometimes happen when working with relationships. To avoid problems, you can check one of three properties in your model context to see exactly what data has been changed since the last save:

  • Read insertedModelsArray to see which model objects have been created.
  • Read changedModelsArray to see which model objects have been edited.
  • Read deletedModelsArray to see which model objects have been deleted, or read isDeleted directly on a model object.

Each of these have equivalents in Core Data's NSManagedObject: isInserted, isUpdated, or isDeleted.

Alternatively, you can read the hasChanges property of a model object, which will be true if the object has been inserted, deleted, or edited since the last save.


이찬희 (MarkiiimarK)
Never Stop Learning.