0
私はidとdetailTextで作られたコアデータエンティティを持っています。アイデアは、私のテキストフィールドは、私のケースではタイムスタンプである特定のIDと一緒にコンテンツを格納しているということです。ユーザーがdetailTextを最初に保存したときには、これが正常に機能しますが、ユーザーが特定のIDのdetailTextを編集した場合、古いdetailTextを削除して新しいdetailTextを挿入します。 基本的に私は以下の質問をSwift/Coreデータに書くのに助けが必要です。特定のIDのコアデータキーの値を編集
UPDATE myEntity SET detailText = theNewDetailText WHERE ID = myID;
これらは私が私のIDを選択見つけるために述語を使用しますが、私は特定のIDのために対応するdetailTextフィールドを更新悩みを抱えている、私の現在の保存と交換する機能です。
func saveTextFromTextFieldToCoreData(textToSave: String){
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("GoalDetail", inManagedObjectContext:managedContext)
let thingToSaveToCD = NSManagedObject(entity: entityDescription!, insertIntoManagedObjectContext: managedContext)
print("received text to store and it's \(textToSave)")
if entityIsEmpty("GoalDetail") == true {
thingToSaveToCD.setValue(data.timeStamp, forKey: "id")
thingToSaveToCD.setValue(textToSave, forKey: "detailText")
print("succesfully saved \(data.timeStamp) and \(textToSave)")
} else {
replaceTheValueFromCoreData()
}
}
func replaceTheValueFromCoreData(){
print("i have entered the delete core data item")
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "GoalDetail")
let predicate = NSPredicate(format: "id == %@", data.timeStamp)
fetchRequest.predicate = predicate
//let entityDescription = NSEntityDescription.entityForName("GoalDetail", inManagedObjectContext:managedContext)
//let thingToSaveToCD = NSManagedObject(entity: entityDescription!, insertIntoManagedObjectContext: managedContext)
do{
let goal = try appDelegate.managedObjectContext.executeFetchRequest(fetchRequest)
try managedContext.save()
}
catch{
//error
}
私はGoogleと、スタックオーバーフローを進めてきたが、これまで任意のヘルプは大歓迎ですので、これを実行するために管理していません。