How can I delete object from core data in swift 3

后端 未结 1 1396
攒了一身酷
攒了一身酷 2020-12-07 04:41

This is how I save data to core, now I want to delete specific object from whole data. How can I do that?
This is how I save data:

func saveProductDetail         


        
相关标签:
1条回答
  • 2020-12-07 05:24

    Check this code for swift 3 core data operations

    import CoreData
    
    class CoreDataOperations: NSObject {
    
        // MARK: Save data
        func saveData() -> Void {
            let managedObjectContext = getContext()
            let personData = NSEntityDescription.insertNewObject(forEntityName: "Person", into: managedObjectContext) as! Person
            personData.name = "Raj"
            personData.city = "AnyXYZ"
    
            do {
                try managedObjectContext.save()
                print("saved!")
            } catch let error as NSError  {
                print("Could not save \(error), \(error.userInfo)")
            } catch {
    
            }
    
        }
    
        // MARK: Fetching Data
        func fetchData() -> Void {
    
            let moc = getContext()
            let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person")
    
            do {
                let fetchedPerson = try moc.fetch(fetchRequest) as! [Person]
    
                print(fetchedPerson.count)
                for object in fetchedPerson {
                    print(object.name!)
                }
    
            } catch {
                fatalError("Failed to fetch employees: \(error)")
            }
    
        }
    
    
    
        // 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: Update Data
        func updateRecords() -> 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 {
                    object.name! = "\(object.name!) Joshi"
                    print(object.name!)
                }
                do{
                    try moc.save()
                    print("saved")
                }catch let error as NSError {
                    print("Could not save \(error), \(error.userInfo)")
                }
    
    
        }
    
        // MARK: Get Context
    
        func getContext () -> NSManagedObjectContext {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            return appDelegate.persistentContainer.viewContext
        }
    }
    

    You can get more from https://github.com/rajkumar24u/CoreDataOperations

    0 讨论(0)
提交回复
热议问题