Swift更改Coredata的值,而不是创建新的

时间:2017-02-15 11:05:53

标签: ios swift core-data

我是新手,对任何objC都不了解,也没有快速的啧啧。

我正在为周计划做app,我有按钮,每天都把我加入 -名称 -代码名称 -Task01 。 。 -Task10

但是如果我想用另一个按钮编辑一个特定的日子,我总是会删除其余的日子。我用很多nils创建新实体,只有一天充满了信息。这是我的贫民窟解决方案,但它不正常,它工作了几次,然后它说它有大约512 searchResults.count并开始滞后,不工作......

请帮助我,如何更改特定属性的值,不要创建新实体

这是我的周二编辑代码(本周其余部分与星期一相同,但是它的长代码,所以这里只有星期一和星期二的代码):

func getAndPrintData () {

    let fetchRequest: NSFetchRequest<Workout> = Workout.fetchRequest()

    do {
        let searchResults = try getContext().fetch(fetchRequest)
        print ("num of results = \(searchResults.count)")
        for works in searchResults as [NSManagedObject] {


            if let moc = managedObjectContext {
                let workout = Workout(context: moc)

                workout.mondayNameCD = works.value(forKey: "mondayNameCD") as! String?
                workout.mondayCodeNameCD = works.value(forKey: "mondayCodeNameCD") as! String?
                workout.mondayTask01 = works.value(forKey: "mondayTaks01") as! String?
                workout.mondayTask02 = works.value(forKey: "mondayTaks02") as! String?
                workout.mondayTask03 = works.value(forKey: "mondayTaks03") as! String?
                workout.mondayTask04 = works.value(forKey: "mondayTaks04") as! String?
                workout.mondayTask05 = works.value(forKey: "mondayTaks05") as! String?
                workout.mondayTask06 = works.value(forKey: "mondayTaks06") as! String?
                workout.mondayTask07 = works.value(forKey: "mondayTaks07") as! String?
                workout.mondayTask08 = works.value(forKey: "mondayTaks08") as! String?
                workout.mondayTask09 = works.value(forKey: "mondayTaks09") as! String?
                workout.mondayTask10 = works.value(forKey: "mondayTaks10") as! String?
                workout.tuesdayNameCD = DayName.text!
                workout.tuesdayCodeNameCD = CodeNameTextField.text!
                workout.tuesdayFocusCD = IconImageTextField.text!
                workout.tuesdayTask01 = Task01TextField.text!
                workout.tuesdayTask02 = Task02TextField.text!
                workout.tuesdayTask03 = Task03TextField.text!
                workout.tuesdayTask04 = Task04TextField.text!
                workout.tuesdayTask05 = Task05TextField.text!
                workout.tuesdayTask06 = Task06TextField.text!
                workout.tuesdayTask07 = Task07TextField.text!
                workout.tuesdayTask08 = Task08TextField.text!
                workout.tuesdayTask09 = Task09TextField.text!
                workout.tuesdayTask10 = Task10TextField.text!
            }
        }
    } catch {
        print("Error with request: \(error)")
    }
    saveToCoreData() {
        self.navigationController!.popToRootViewController(animated: true)
    }
}

func saveToCoreData(completion: @escaping ()->Void){
    managedObjectContext!.perform {
        do {
            try self.managedObjectContext?.save()
            completion()
            print("Product saved to CoreData")
        } catch let error {
            print("Could not save Product to CoreData: \(error.localizedDescription)")
        }
    }
}

var managedObjectContext: NSManagedObjectContext? {
    return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}

func getContext () -> NSManagedObjectContext {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    return appDelegate.persistentContainer.viewContext
}

1 个答案:

答案 0 :(得分:0)

在for循环中,您已经拥有了一个托管对象works。只需更新其中的值,而不是创建新的Workout