【发布时间】:2016-06-11 20:32:24
【问题描述】:
我正在用 Swift 编写一个时间跟踪应用程序,但在删除某项活动所花费的时间时遇到了问题。我的删除功能在 2 个视图上将其从 tableView 中隐藏,并且似乎将其从 Core Data 中删除,但时间并没有从我的“今天花费的总小时数”功能中删除。
历史删除代码:
func tableView(tableView: UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let historyToDelete = fetchController.objectAtIndexPath(indexPath)
let main = MainViewController()
var totalDuration = main.totalduration
let historyDel = fetchController.objectAtIndexPath(indexPath) as! History
totalDuration = totalDuration - Int(historyDel.duration!)
CoreDataHandler.sharedInstance.deleteObject(historyToDelete as! NSManagedObject)
}
}
CoreDataHandler中删除代码:
func deleteObject(object: NSManagedObject) {
backgroundManagedObjectContext.deleteObject(object)
saveContext()
}
计算一天总持续时间的函数:
func calculateTotalDurationForToday() -> NSInteger {
var sumOfDuration = 0
for history in todaysActivitiesArray {
sumOfDuration += (history.duration?.integerValue)!
}
return sumOfDuration
}
如果我关闭模拟器并再次运行,今天花费的总时间会删除对象,因此在我关闭应用程序之前不能触发它。有关如何解决此问题的任何建议?
【问题讨论】:
标签: ios swift uitableview core-data nsdate