替换 CoreData 中的属性
Posted
技术标签:
【中文标题】替换 CoreData 中的属性【英文标题】:replacing attribute within CoreData 【发布时间】:2016-03-12 14:55:45 【问题描述】:我在 CoreDate 中有一个名为 BudgetDetail 的实体。我有下面的代码,我想更新属性“剩余”,但它似乎是添加新记录而不是更新。
func checksBudgetDetail(budgetName: String, spendAmount: Double, date: NSDate)
print("budget name = \(budgetName)")
//fetch results fron income entity and place in array
let budgetDetailResults: [AnyObject] = try! context.executeFetchRequest(budgetDetail)
print("budget results loaded")
//if there are no incomes saved, do nothing.
if budgetDetailResults.isEmpty
//if there are incomes saved, retrieve name and amount
else
for i in budgetDetailResults
print("in loop")
let name = (i.valueForKey(budgetNameKeyToUpdate)! as! String)
let amount = Double(i.valueForKey(budgetAmountKeyToUpdate)!.stringValue)
let durationType = (i.valueForKey(budgetDurationTypeKeyToUpdate)! as! String)
let durationOccurance = Double(i.valueForKey(budgetDurationOccuranceKeyToUpdate)!.stringValue)
let remaining = (i.valueForKey(budgetRemaningKeyToUpdate)!.stringValue)
let startDate = (i.valueForKey(startDateKeyToUpdate)! as! NSDate)
let endDate = (i.valueForKey(endDateKeyToUpdate)! as! NSDate)
if name == budgetName
print("just logged a spend against the budget \(name)")
//if the date in the on the spend is within the startDate and endDate, call the updateBudgetDetailEntity to update the remaining budget
if startDate.compare(date) == .OrderedAscending && endDate.compare(date) == .OrderedDescending
print("the spend which has just been logged is also within the dates of \(name)'s budget")
let newRemaining : Double
print("\(budgetName) has a old reamining amount of:\(remaining)")
newRemaining = Double(remaining)! - spendAmount
print("\(budgetName) has a new reamining amount of:\(newRemaining)")
let entity = NSEntityDescription.entityForName("BudgetDetail", inManagedObjectContext: context)!
let saveBudgetEntity = NSManagedObject(entity: entity, insertIntoManagedObjectContext:context)
// add user input to the relevant entity
saveBudgetEntity.setValue(name, forKey: budgetNameKeyToUpdate)
saveBudgetEntity.setValue(amount, forKey: budgetAmountKeyToUpdate)
saveBudgetEntity.setValue(durationType, forKey: budgetDurationTypeKeyToUpdate)
saveBudgetEntity.setValue(durationOccurance, forKey: budgetDurationOccuranceKeyToUpdate)
saveBudgetEntity.setValue(startDate, forKey: startDateKeyToUpdate)
saveBudgetEntity.setValue(endDate, forKey: endDateKeyToUpdate)
saveBudgetEntity.setValue(newRemaining, forKey: budgetRemaningKeyToUpdate)
do
try context.save()
print("new remaining attriute saved")
catch _
print("new remaining attriute didnt saved")
else
//would need to create a new record with new start and end dates
else
print("\(date)")
print("Name:\(budgetName), startDate:\(startDate), endDate:\(endDate)")
看起来好像添加了一条新记录,而不是在循环中更新一条记录。我不确定为什么会这样,任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:这些行:
let entity = NSEntityDescription.entityForName("BudgetDetail", inManagedObjectContext: context)!
let saveBudgetEntity = NSManagedObject(entity: entity, insertIntoManagedObjectContext:context)
// add user input to the relevant entity
saveBudgetEntity.setValue(name, forKey: budgetNameKeyToUpdate)
saveBudgetEntity.setValue(amount, forKey: budgetAmountKeyToUpdate)
saveBudgetEntity.setValue(durationType, forKey: budgetDurationTypeKeyToUpdate)
saveBudgetEntity.setValue(durationOccurance, forKey: budgetDurationOccuranceKeyToUpdate)
saveBudgetEntity.setValue(startDate, forKey: startDateKeyToUpdate)
saveBudgetEntity.setValue(endDate, forKey: endDateKeyToUpdate)
saveBudgetEntity.setValue(newRemaining, forKey: budgetRemaningKeyToUpdate)
创建一个新的托管对象并设置其属性值。如果您只想更新现有对象,请删除这些行并替换为:
i.setValue(newRemaining, forKey: budgetRemaningKeyToUpdate)
【讨论】:
以上是关于替换 CoreData 中的属性的主要内容,如果未能解决你的问题,请参考以下文章
我如何(或应该?)用嵌套的 managedObjectContext 替换这个 CoreData SwiftUI 应用程序中的 MVVM?