63

Unfortunately the new Core Data semantics make me crazy. My previous question had a clean code that didn't work because of incorrect auto generation of header files. Now I continue my work with deleting objects. My code seems to be very simple:

func deleteProfile(withID: Int) {
    let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
    fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")
    let object = try! context.fetch(fetchRequest)
    context.delete(object)
} 

I did a "hard" debug with print(object) instead of context.delete(object) and it showed me the right object. So I need just to delete it.

P.S. there is no deleteObject. Now NSManagedContext has only public func delete(_ sender: AnyObject?)

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    Unless things have changed in Swift 3 (apologies if they have), the fetch will return an array of objects even if there is only one that matches your predicate. You need either to loop through the results (safest) or use `object[0]` to access the first object in the array. – pbasdf Jun 24 '16 at 16:06

10 Answers10

112

The result of a fetch is an array of managed objects, in your case [Event], so you can enumerate the array and delete all matching objects. Example (using try? instead of try! to avoid a crash in the case of a fetch error):

if let result = try? context.fetch(fetchRequest) {
    for object in result {
        context.delete(object)
    }
}

do {
    try context.save()
} catch {
    //Handle error
}

If no matching objects exist then the fetch succeeds, but the resulting array is empty.


Note: In your code, object has the type [Event] and therefore in

context.delete(object)

the compiler creates a call to the

public func delete(_ sender: AnyObject?)

method of NSObject instead of the expected

public func delete(_ object: NSManagedObject)

method of NSManagedObjectContext. That is why your code compiles but fails at runtime.

mehdigriche
  • 444
  • 1
  • 4
  • 14
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
60

The trick here, it is save context after deleting your objects.

let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")
let objects = try! context.fetch(fetchRequest)
for obj in objects {
    context.delete(obj)
}

do {
    try context.save() // <- remember to put this :)
} catch {
    // Do something... fatalerror
}

I hope this can help someone.

Alex Zavatone
  • 4,106
  • 36
  • 54
J. Lopes
  • 1,336
  • 16
  • 27
  • 4
    Getting error:- `Cannot convert value of type '[Entity]' to expected argument type 'NSManagedObject'` on the line `context.delete(object)` – amish Jan 01 '18 at 08:31
  • @amish the fetchRequest returns an Array. So the var should be called objects. After you just do a for loop (for object in objects) and in the for loop you can delete each object – Sonius Mar 07 '18 at 10:29
24
func deleteRecords() {
    let delegate = UIApplication.shared.delegate as! AppDelegate
    let context = delegate.persistentContainer.viewContext

    let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "nameofentity")
    let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)

    do {
        try context.execute(deleteRequest)
        try context.save()
    } catch {
        print ("There was an error")
    }
}
Hitesh Chauhan
  • 1,520
  • 15
  • 16
  • 2
    This is _way_ better than most of the answers here! > "A request to Core Data to do a batch delete of data in a persistent store without loading any data into memory." – Firo Aug 08 '18 at 18:55
  • 2
    @Firo This is actually the best answer, unless you need to write code that works before 10.11 as before 10.11 NSBatchDeleteRequest didn't exist. In any other case, this will make CoreData directly delete the data in the database, skipping all processing to load any data first and skipping any memory usage for storing the data after the fetch. – Mecki Feb 13 '19 at 17:57
21

Delete core data objects swift 3

// MARK: Delete Data Records

func deleteRecords() -> Void {
    let moc = getContext()
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person")

     let result = try? moc.fetch(fetchRequest)
        let resultData = result as! [Person]

        for object in resultData {
            moc.delete(object)
        }

        do {
            try moc.save()
            print("saved!")
        } catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        } catch {

        }

}

// MARK: Get Context

func getContext () -> NSManagedObjectContext {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    return appDelegate.persistentContainer.viewContext
} 
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37
5

Swift 4.1, 4.2 and 5.0

     let appDelegate = UIApplication.shared.delegate as! AppDelegate
     let context = appDelegate.persistentContainer.viewContext
     let requestDel = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
     requestDel.returnsObjectsAsFaults = false
  // If you want to delete data on basis of some condition then you can use NSPredicate
  //  let predicateDel = NSPredicate(format: "age > %d", argumentArray: [10])
  // requestDel.predicate = predicateDel


     do {
          let arrUsrObj = try context.fetch(requestDel)
          for usrObj in arrUsrObj as! [NSManagedObject] { // Fetching Object
              context.delete(usrObj) // Deleting Object
         }
     } catch {
          print("Failed")
     }

    // Saving the Delete operation
     do {
         try context.save()
     } catch {
         print("Failed saving")
     }
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58
4

Swift 4 without using string for Entity

let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")

do {
    let objects = try context.fetch(fetchRequest)
    for object in objects {
        context.delete(object)
    }
    try context.save()
} catch _ {
    // error handling
}
Sonius
  • 1,597
  • 3
  • 14
  • 33
3

Swift 5

Common function for Deleting core data objects for any anity swift 5

func deleteEntityObjectByKeyValue<T>(className: T.Type, key: String, value: Any) -> Bool {
        let context = CoreDataStack.sharedStack.mainContext
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: className.self))

        //Search based on data type.I used for Int and String
        if  let sValue = value as? String {
            let predicate = NSPredicate(format: "\(key) == %@", sValue)
            fetchRequest.predicate = predicate
        } else if let iValue = value as? Int64 {
            let predicate = NSPredicate(format: "\(key) == %d", iValue)
            fetchRequest.predicate = predicate
        }
        do {
            let result = try context.fetch(fetchRequest)
            if result.count != 0 {
                if let managedObject = result[0] as? NSManagedObject {
                    context.delete(managedObject)
                    do {
                        try context.save()
                        return true
                    }
                    catch let error {
                        print(error.localizedDescription)
                    }
                }
            }
            return false
        } catch let error {
            print(error.localizedDescription)
        }
        return false
    }

How To Use it:

let isSaved = CoreDataOperations.shared.deleteEntityObjectByKeyValue(className: EmpolyeeData.self, key: "employeeId", value:1234)
If isSaved {
    print("Deleted obj Successfully")
}
Priyank Patel
  • 791
  • 8
  • 6
2

Delete Core Data Object with query in Swift 5, 4.2

let fetchRequest = NSFetchRequest<Your_Model>(entityName: "Your_Entity_Name")
fetchRequest.predicate = NSPredicate(format: "any your_key == %d", your_value)

hope this will help to someone

caldera.sac
  • 4,918
  • 7
  • 37
  • 69
1

Swift 4,5

It is useful to delete particular record or all records from entity.

1.Create a NSPersistentContainer and NSManagedObjectContext using below code.

class CoreDataStack: NSObject {
    
    static var sharedStack = CoreDataStack()
    private override init() {}
    
    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "Employee")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let url = storeDescription.url {
                print("SQLITE STORE LOCATION: \(url.absoluteString)")
            }
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        container.viewContext.shouldDeleteInaccessibleFaults = true
        container.viewContext.automaticallyMergesChangesFromParent = true
        return container
    }()
    private lazy var applicationDocumentsDirectory: URL = {
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return urls[urls.count-1]
    }()
    private lazy var managedObjectModel: NSManagedObjectModel = {
        let modelURL = Bundle.main.url(forResource: "____", withExtension: "____")!
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()
    public lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.appendingPathComponent("Employee" + ".sqlite")
        var failureReason = "There was an error creating or loading the application's saved data."
        do {
            let options = [ NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true ]
            try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
        } catch {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
            dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
            
            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
            // Replace this with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
        }
        
        return coordinator
    }()

    lazy var mainContext: NSManagedObjectContext = {
        var managedObjectContext: NSManagedObjectContext?
        if #available(iOS 10.0, *){
            managedObjectContext = self.persistentContainer.viewContext
            managedObjectContext?.mergePolicy = NSMergePolicy.init(merge: NSMergePolicyType.mergeByPropertyObjectTrumpMergePolicyType)
        }
        else{
            // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
            let coordinator = self.persistentStoreCoordinator
            managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
            managedObjectContext?.persistentStoreCoordinator = coordinator
            managedObjectContext?.mergePolicy = NSMergePolicy.init(merge: NSMergePolicyType.mergeByPropertyObjectTrumpMergePolicyType)

        }
        return managedObjectContext!
    }()

   }

2.Common function for Deleting core data all objects for any Entity swift 5

    func deleteEntityData(entity : String) {
        let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
        let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
        do {
            try CoreDataStack.sharedStack.mainContext.execute(deleteRequest)
            CoreDataStack.sharedStack.saveMainContext()
        } catch {
            print ("There was an error")
        }
}

3.How to use above code.

self.deleteEntityData(entity : "Employee")

4.If you want to delete Particular object from Entity

func deleteEntityObjectByKeyValue<T>(entityName: T.Type, key: String, value: Any) -> Bool {
        let context = CoreDataStack.sharedStack.mainContext
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: entityName.self))
        if  let sValue = value as? String {
            let predicate = NSPredicate(format: "\(key) == %@", sValue)
            fetchRequest.predicate = predicate
        } else if let iValue = value as? Int64 {
            let predicate = NSPredicate(format: "\(key) == %d", iValue)
            fetchRequest.predicate = predicate
        }
        do {
            let result = try context.fetch(fetchRequest)
            if result.count != 0 {
                if let managedObject = result[0] as? NSManagedObject {
                    context.delete(managedObject)
                    do {
                        CoreDataStack.sharedStack.saveMainContext()
                        return true
                    }
                    catch let error {
                        print(error.localizedDescription)
                    }
                }
            }
            return false
        } catch let error {
            print(error.localizedDescription)
        }
        return false
    }
Priyank Patel
  • 791
  • 8
  • 6
0

Delete the object from core data

let entity = NSEntityDescription.entity(forEntityName: "Students", in: managedContext)
        let request = NSFetchRequest<NSFetchRequestResult>()
        request.entity = entity
        if let result = try? managedContext.fetch(request) {
            for object in result {
                managedContext.delete(object as! NSManagedObject)
            }
            txtName.text = ""
            txtPhone.text = ""
            txt_Address.text = ""
            labelStatus.text = "Deleted"

        }
Srinivasan_iOS
  • 972
  • 10
  • 12