SwiftData - delete all instances of model causes "Type cannot conform to PersistentModel" error

Background

I have a SwiftData Model specified like this, within a SwiftUI app:

@Model
class MyModel
{
    // Model information.
}

It's been working as expected. I can add it to my app's model container as follows:

WindowGroup
{
    ContentView()
}
.modelContainer(for: [MyModel.self])

And have been able to create, persist, and fetch data as expected.


Problem

I created a function to delete the persisted data:

func deleteData(modelContext: ModelContext)
{
    modelContext.delete(MyModel.self)
}

But the compiler is throwing this error:

Type 'MyModel.Type' cannot conform to 'PersistentModel'.

I'm attempting to do this based of the documentation which specifies that only a model may be provided to delete all instances:

Warning If you don’t provide a predicate, the context will remove all models of the specified type from the persistent storage.


Other Information

Is this a bug in Xcode or the Swift compiler? It makes no sense at all, for the following reasons.

MyModel is explicitly PersistentModel

I can adjust my model to the following without any errors:

@Model
class MyModel: PersistentModel
{
    // Model information.
}

So clearly MyModel does conform to PersistentModel, but it doesn't clear the error.

(Note: There are no compiler errors that pop up, but there actually will be a Preview error that says Redundant conformance of 'MyModel' to protocol 'PersistentModel', which is helpful as more confirmation that it conforms either way.)

.modelContainer(for:) accepts PersistentModel

I'm calling .modelContainer(for: [MyModel.self]) without any issues, and the documentation for this function confirms that it accepts a PersistentModel for parameter.

The documentation for ModelContainer has these initializers:

init(for: Schema, migrationPlan: (any SchemaMigrationPlan.Type)?, configurations: [ModelConfiguration]) throws

convenience init(for: any PersistentModel.Type..., migrationPlan: (any SchemaMigrationPlan.Type)?, configurations: ModelConfiguration...) throws

convenience init(for: Schema, migrationPlan: (any SchemaMigrationPlan.Type)?, configurations: ModelConfiguration...) throws

One of them explicitly accepts any PersistentModel.Type, and the other two accept Schema, whose documentation shows the initializer that I seem to be using, which also accepts any PersistentModel.Type:

init([any PersistentModel.Type], version: Schema.Version)

Conclusion

Overall, it seems like this might be a bug since the class can be explicitly labeled as PersistentModel without errors, and it's already being used with functions that require a PersistentModel parameter.

What can I do about this?

I found this answer from about a year ago, but none of the solutions seem applicable. One of them mentions a bug that was supposedly fixed. I'm on Xcode 15.4.

Answered by joadan in 800668022

You are calling the wrong method, instead do

modelContext.delete(model: MyModel.self)
Accepted Answer

You are calling the wrong method, instead do

modelContext.delete(model: MyModel.self)
SwiftData - delete all instances of model causes "Type cannot conform to PersistentModel" error
 
 
Q