I am a little confused as to how to delete all core data in swift. I have created a button with an IBAction
linked. On the click of the button I have the follow
In Swift UI
when you select core data at the beginning of project the persistence coordinator is automatically created by swift in app delegate. The scene delegate creates @environmentObject for managed context
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let contentView = ContentView().environment(\.managedObjectContext, context)
create instance of manageObjectContext in content view and also fetchRequest object using property wrapper @FetchRequest
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: EntityName.entity(), sortDescriptors: []) var entityNames: FetchedResults
For delete action perform
for entityName in entityNames {
moc.delete(entityName)
}
try? moc.save()
try operation can throw error so please implement do try catch block in production code to handle error properly.