2016-11-23 164 views
1

這是我如何將數據保存到核心,現在我想從整個數據中刪除特定的對象。我怎樣才能做到這一點?
這是我如何保存數據:如何從swift中的核心數據中刪除對象3

func saveProductDetails(product : Product) { 
     // set user_id 
     product.user_id = UserDefaults.sharedInstace.getCustomerId() 
     //save data locally for the time been 
     let entity = NSEntityDescription.entityForName("Product", inManagedObjectContext: self.writeContext) 
     let category = NSEntityDescription.entityForName("Category", inManagedObjectContext: self.writeContext) 
     let brand = NSEntityDescription.entityForName("Brand", inManagedObjectContext: self.writeContext) 

     var entityProduct = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:self.writeContext) 
     var entityCategory = NSManagedObject(entity: category!, insertIntoManagedObjectContext:self.writeContext) 
     var entityBrand = NSManagedObject(entity: brand!,insertIntoManagedObjectContext: self.writeContext) 

     entityProduct = product.settingManagedObject(entityProduct) 
     entityProduct.setValue(String(Utill.sharedInstace.getTimeStamp()), forKey: "time_stamp") 
     entityProduct.setValue(Constant.Status.STATUS_NOT_VERIFIED, forKey: "status") 


     if product.categoryObject != nil{ 
      product.categoryObject.user_id = UserDefaults.sharedInstace.getCustomerId(); 
      entityCategory = product.categoryObject.settingManagedObject(entityCategory) 
     } 

     if product.brandObject != nil{ 
      product.brandObject.user_id = UserDefaults.sharedInstace.getCustomerId(); 
      entityBrand = product.brandObject.settingManagedObject(entityBrand) 
     } 

     entityProduct.setValue(entityCategory, forKey:"category") 
     entityProduct.setValue(entityBrand, forKey: "brand") 

     writeContext.performBlock { 
      do { 

       try self.writeContext.save() 

       self.managedContext.performBlock({ 
        do{ 
         try self.managedContext.save() 
        } catch let error as NSError { 
         print("Could not save \(error), \(error.userInfo)") 
        } 

       }) 
      } catch let error as NSError { 
       print("Could not save \(error), \(error.userInfo)") 
       return 
      } 

     } 

    } 

Product對象有兩個人的關係,我想刪除只有特定的對象,不是全部。這意味着刪除(其中product.purchase_id == "selected_purchse_id"),在UITableView
如何做到這一點?

+0

見CoreData教程:https://iosdevcenters.blogspot.com/2016/06/crud- operations-using-code-data-swift.html –

回答

8

檢查此代碼爲SWIFT 3核心數據業務

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

你可以得到更多的從https://github.com/rajkumar24u/CoreDataOperations

+0

寫得很好很好寫 – Tiger

+0

謝謝@Tiger –

+0

我喜歡簡單和重點 –

相關問題