2017-02-15 53 views
0

我是新的,不理解任何objC,并没有迅速的啧啧。迅速改变Coredata的价值,而不是创造新的

我做一周刨应用程序,我有按钮,在加我每天 -name -codeName -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 
} 

回答

0

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