SwiftData crashes when trying to access a deleted object

Oscar de la Hera Gomez
Three flowers that represent Swift, SwiftData and Xcode. Beneath them sits the text "isDeleted."

Verify that the isDeleted parameter of a SwiftData object is false before reading parameters to avoid an app from crashing after an object has been deleted.

If your system attempts to access a SwiftData object after it has been deleted, the app will crash and the stack trace will point to the parameter that you are trying to access in the SwiftData model.

For example, the code below will cause a crash when an app tries to access the id parameter of an object that has been deleted.

if let object = self.object, let id = object.id {}

To solve this crash, use the isDeleted parameter, as shown below.

if let object = self.object, !object.isDeleted, let id = object.id {}

Looking to learn more about things you can do with SwiftData, Swift or XCode ?

Consult our quick start guide or search our blog to find educational content on learning how to use SwiftData, Swift or XCode.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details