CoreData Basics in Swift - All Functions

CoreData Basics in Swift - All Functions 

First we need to import CoreData in header. 

import CoreData

then Add Variables (Globally) 

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    var context : NSManagedObjectContext?

 Initials value in viewDidLoader

    context = appDelegate.persistentContainer.viewContext


Fetch :


 func fetch(){
        
        let request = NSFetchRequest<NSFetchRequestResult>(entityName : "FoodInCart")
        
        request.returnsObjectsAsFaults = false
        
        do{
          let results = try context?.fetch(request)
            
            let count = results?.count
            
            print(count!)
            
            if  0 < count! {
                for result in results! as! [NSManagedObject]
                {
                    print("No of \(String(describing: count))--------")
                    if let dishid = result.value(forKey: "dishid") as? String{
                        print("==========DishId=========")
                        print(dishid)
                        print("==========Count=========")
                    }
                    if let count = result.value(forKey: "count") as? String{
                        print(count)
                    }
                }
            }
            
        }catch{
            
            print("Error in Saving the Object")
            
        }

    }

ADD / Save :

func Save(dishId : String, count : String, Price : String){
        
        let foodProduct = NSEntityDescription.insertNewObject(forEntityName: "FoodInCart", into: context!)
        
        foodProduct.setValue(dishId, forKey: "dishid")
        foodProduct.setValue(count, forKey: "count")
        foodProduct.setValue(Price, forKey: "price")
        
        do{
            
            try context?.save()
            
            print("Save Succesfully")
            
            self.fetch()
            
        }catch{
            
            print("Error in Saving the Object")
            
        }

    }

Update :

func Update(dishId : String, count : String){
        
        let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "FoodInCart")
        
        let predicate = NSPredicate(format: "dishid = %@", dishId)
        
        fetchRequest.predicate = predicate
        
        do
        {
            
            self.fetch()
            
            let test = try context?.fetch(fetchRequest)
            
            if test?.count == 1
            {
                let objectUpdate = test![0] as! NSManagedObject
                
                objectUpdate.setValue(count, forKey: "count")
                
                do{
                    try context?.save()
                    print("***************************************")
                    print("Update Successfully")
                    
                    self.fetch()
                    
                }
                catch
                {
                    print("unable to update")
                }
            }
        }
        catch
        {
            print("unable to update")
        }
        

    }

Delete : 

    func deleteDish(dishID : String){
        
        let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "FoodInCart")
        
        let predicate = NSPredicate(format: "dishid = \(dishID)")
        
        fetchRequest.predicate = predicate
        
        self.fetch()
        
        let object = try! context?.fetch(fetchRequest)
        
        if object?.count == 1
        {
            let objectUpdate = object![0] as! NSManagedObject
            
            context?.delete(objectUpdate)
            
            do {
                try context?.save()
                
                print("***************************************")
                
                print("Deleted Succesfully")
                
                self.fetch()
                
            } catch {
                
                print("Unable to Delete Record")
                
            }
            
        }
        

    }

Comments