Swift 3 Core Data Delete Object

后端 未结 8 2132
日久生厌
日久生厌 2020-11-29 02:41

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

相关标签:
8条回答
  • 2020-11-29 03:06

    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"
    
            }
    
    0 讨论(0)
  • 2020-11-29 03:08

    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.

    0 讨论(0)
  • 2020-11-29 03:11

    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

    0 讨论(0)
  • 2020-11-29 03:19

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

    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.

    0 讨论(0)
  • 2020-11-29 03:24

    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
    }
    
    0 讨论(0)
  • 2020-11-29 03:26
    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")
        }
    }
    
    0 讨论(0)
提交回复
热议问题