2017-02-15 56 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