私はSwiftに時間トラッキングアプリを書いていますが、アクティビティに費やされた時間を削除することに問題があります。私の削除機能はtableViewからビューを2回隠し、コアデータから削除するように見えますが、「今日の総時間」機能から時間は削除されません。Core Dataオブジェクトを削除しても、まだNSFetchedResultsControllerに表示されますか?
履歴から削除するためのコード。私は、シミュレータを閉じると
func calculateTotalDurationForToday() -> NSInteger {
var sumOfDuration = 0
for history in todaysActivitiesArray {
sumOfDuration += (history.duration?.integerValue)!
}
return sumOfDuration
}
と:今日の活動の配列を取得するため
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let historyToDelete = fetchController.objectAtIndexPath(indexPath)
let todaysActivitiesArray = CoreDataHandler.sharedInstance.fetchCoreDataForTodayActivities()
let main = MainViewController()
var totalDuration = main.calculateTotalDurationForToday()
let historyDel = fetchController.objectAtIndexPath(indexPath) as! History
totalDuration = totalDuration - Int(historyDel.duration!)
CoreDataHandler.sharedInstance.deleteObject(historyToDelete as! NSManagedObject)
main.totalduration = totalDuration
}
}
コード:今日の活動を計算するための
func fetchCoreDataForTodayActivities() -> [History] {
let fetchRequest = NSFetchRequest()
let entityDescription = NSEntityDescription.entityForName("History", inManagedObjectContext: self.backgroundManagedObjectContext)
fetchRequest.entity = entityDescription
let dateDescriptor = NSSortDescriptor(key: "startDate", ascending: false)
fetchRequest.sortDescriptors = [dateDescriptor]
let startDate = NSDate.dateByMovingToBeginningOfDay()
let endDate = NSDate.dateByMovingToEndOfDay()
let predicate = NSPredicate(format: "(startDate >= %@) AND (startDate <= %@)", startDate, endDate)
fetchRequest.predicate = predicate
return (fetchCoreDataWithFetchRequest(fetchRequest) as! [History])
}
コードをこれには二つの異なるビューコントローラでのtableViewのために働きますもう一度実行して合計時間が減算されるので、私はそれを閉じるまで削除を開始してはいけません。助けてください、私は今この間に立ち往生しています。
削除をコミットするためにコンテキストを保存することがあります。 'main'は、ストーリーボードに期待されるものではなく、' MainViewController'の新しいインスタンスです。 – vadian