CoreData不会从视图中删除,但会从数据库中删除

时间:2017-12-25 09:19:24

标签: ios swift sqlite core-data swift3

我为我的应用创建个人资料,当我试图删除个人资料时,如果我回到它,它将不会从视图中删除,另一方面,如果我关闭应用并再次打开它,数据将是不见了。我可以看到数据已从我的模拟器中的sqlite数据库中删除。

override func viewDidLoad() {
    super.viewDidLoad()
    showDatePicker()
    showPickerRanks ()
    pickerRank.delegate = self
    pickerRank.dataSource = self
    sideMenus()

    let appDelegate = UIApplication.shared.delegate as! AppDelegate // UIApplication.shared().delegate as! AppDelegate is now UIApplication.shared.delegate as! AppDelegate

    let context = appDelegate.persistentContainer.viewContext

    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Profile")

    request.returnsObjectsAsFaults = false

    var isEmpty : Bool {
        do {
            let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Profile")
            let count = try context.count(for: request)
            return count == 0 ? true : false
        } catch {
            return true
        }
    }
    if isEmpty == true {
        //Check if there is a Profile
        performSegue(withIdentifier: "NoProfileSegue", sender: nil)
    } else {
        print("No Files")
    }

    print("Database is empty:\(isEmpty)")

    do {
        let results = try context.fetch(request)
        for result in results as! [NSManagedObject] {
            if let airmanNameVar = result.value(forKey: "airmanName") as? String {
                PersonName.text = airmanNameVar
            }
            if let airmanRankVar = result.value(forKey: "airmanRank") as? String {
                AirmanRankPicker.text = airmanRankVar
            }
            if let airmanHeigthVar = result.value(forKey: "airmanHeight") as? Double {
                HeightTxtLbl.text = String (airmanHeigthVar)
            }
            if let airmanWeightVar = result.value(forKey: "airmanWeight") as? Int64 {
                weightTxtLbl.text = String (airmanWeightVar)
            }
            if let airmanAFPTScore = result.value(forKey: "airmanLastAFPTScore") as? Double {
                LastAFPTScore.text = String(airmanAFPTScore)
            }
            if let airmanAFPTDateLast = result.value(forKey: "airmanLastAFTPData") as? String {
                LastAFPTDate.text = airmanAFPTDateLast
            }

这是我的删除

@IBAction func DeleteBtn(_ sender: Any) {

    let actionSheetController: UIAlertController = UIAlertController(title: "AF Fitness", message:"By pressing Delete all data would be erase.", preferredStyle: .actionSheet)

    //Create and add the Cancel action
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in

    }
    actionSheetController.addAction(cancelAction)
    let AgreeAction: UIAlertAction = UIAlertAction(title: "Delete", style: .default) { action -> Void in
        self.resetAllRecords(in: "Profile")

        self.deleteHistory()

        UserDefaults.standard.set(false, forKey: "ProfileOn") //Bool Data Type
        UserDefaults.standard.synchronize()

        self.AgeGroupSeg.selectedSegmentIndex = 0
        self.GenderSeg.selectedSegmentIndex = 0

        UserDefaults.standard.set(0, forKey: "GenderData")
        UserDefaults.standard.set(0, forKey: "AgeGroupData")

        self.AirmanRankPicker.text = ""
        self.PersonName.text = ""
        self.weightTxtLbl.text = ""
        self.HeightTxtLbl.text = ""
        self.LastAFPTDate.text = ""
        self.LastAFPTScore.text = ""

        self.performSegue(withIdentifier: "NoProfileSegue", sender: nil)
        // self.performSegue(withIdentifier: "NoProfileSegue", sender: nil)
    }
    actionSheetController.addAction(AgreeAction )

     //Present the AlertController
    self.present(actionSheetController, animated: true, completion: nil)
}


func resetAllRecords(in entity : String) // entity = Your_Entity_Name
{
    let appDelegate = UIApplication.shared.delegate as! AppDelegate // UIApplication.shared().delegate as! AppDelegate is now UIApplication.shared.delegate as! AppDelegate

    let context = appDelegate.persistentContainer.viewContext

    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Profile")

    request.returnsObjectsAsFaults = false

    let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
    do {
        try context.execute(deleteRequest)
        try context.save()
    } catch {
        print ("There was an error")
    }
}

提前谢谢

0 个答案:

没有答案