Wie kann ich ein Objekt löschen von core data bei swift 3

Dies ist, wie ich das sichern der Daten auf einem Kern, nun möchte ich das löschen von bestimmten Objekts aus der gesamten Daten. Wie kann ich das tun?

Dies ist, wie ich Daten speichern:

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
            }

        }

    }

Diese Product Objekt hat Beziehung zu zwei anderen, und die ich löschen möchte, nur bestimmten Objekt, nicht alle. Das heißt zu löschen (was die product.purchase_id == "selected_purchse_id"), in UITableView.

Wie macht man das?

InformationsquelleAutor bill | 2016-11-23
Schreibe einen Kommentar