UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

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)
}

3      

SwiftUI does not have NSBatchDeleteRequest or NSFetchRequest.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

But CoreData has. You have to import CoreData.

3      

@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

2      

@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.

2      

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 ?

2      

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.

2      

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

2      

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()

    }

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.