BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: Delete all item from Core Data with Button

Forums > SwiftUI

How can ı delete all item from core data ? Deleting one by one can be tiring sometimes.

   

With a BatchDeleteRequest. Create one for each entity you have and isn't deleted automatically via delete rule "Cascade".

func deleteAll() {
      let fetchRequest1: NSFetchRequest<NSFetchRequestResult> = YourEntityName.fetchRequest()
      let batchDeleteRequest1 = NSBatchDeleteRequest(fetchRequest: fetchRequest1)
      _ = try? container.viewContext.execute(batchDeleteRequest1)
}

   

SwiftUI does not have NSBatchDeleteRequest or NSFetchRequest.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

But CoreData has. You have to import CoreData.

   

@Bnerd  

I tried @Hatsuhira's method on the FriendFace app and it worked perfectly. Before I was using a for loop :P

@Hatshuhira I have a question if you know please, I have a SQL data base browser and although the items are deleted every time, when I recreate them there is Z_PK column that keeps progressing the numbers, i.e.: Initially you enter 100 entries, each has Z_PK 1,2, up to 100 Once you delete, and re-enter the 100 entries, you still have 100 entries, but each entry now has Z_PK that starts from 101,102 etc

   

@Bnerd This is because the PK (primary key) is an autoincrement. I know you can reset them with a SQL command. But I don't know how to do this with Core Data. I'm not even sure you can run a raw SQL command with Core Data. Usually, you shouldn't mess with the database primary keys although when every table is empty there shouldn't be a risk.

The easiest way to reset it in your app is to delete it and reinstall it.

   

Sorry. I don't understand. I am new in swiftUI. I created one function which name is deleteAll. But program said cannot find container in scope. I have one for each in list. I use onDelete to delete one by one. Where should i use this function ? Can you show one example for that ?

   

I solved like this.

  func deleteAll() {
        let fetchRequest1: NSFetchRequest<NSFetchRequestResult> = GeneratedQR.fetchRequest()
        let batchDeleteRequest1 = NSBatchDeleteRequest(fetchRequest: fetchRequest1)
        try? moc.execute(batchDeleteRequest1)
        try? moc.save()

    }

I assigned it to the button. When ı press the button all items delete. But ı cant see dynamically. Pages doesnt rendered. I can show only re open the app.

   

I would have to see the rest of your code and what is the data source for your page?

   

I figured out.

  func deleteAll() {
        //write item type to itemType
        let fetchRequest = itemType.fetchRequest()
        let items = try? moc.fetch(fetchRequest)
        for item in items ?? [] {
            moc.delete(item)
        }
        try? moc.save()

    }

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.