저는 Swift에 시간 추적 앱을 쓰고 있는데, 활동에 소요 된 시간을 삭제하는 데 문제가 있습니다. 내 삭제 기능은 tableView에서 2 개의보기를 숨기고 코어 데이터에서 제거하는 것처럼 보입니다. 그러나 시간은 "오늘 소비 한 총 시간"기능에서 삭제되지 않습니다.핵심 데이터 개체를 삭제해도 여전히 NSFetchedResultsController에 표시됩니까?
기록에서 삭제하기위한 코드. 오늘의 활동 계산
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])
}
코드 : 오늘 활동 배열을 얻기를위한
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
}
}
코드 : 이것은 두 개의 서로 다른 뷰 컨트롤러에있는 tableView 작동 내가 시뮬레이터를 닫으면
func calculateTotalDurationForToday() -> NSInteger {
var sumOfDuration = 0
for history in todaysActivitiesArray {
sumOfDuration += (history.duration?.integerValue)!
}
return sumOfDuration
}
및 다시 합계 시간을 예상대로 뺍니다. 그래서 그것을 닫을 때까지 삭제가 트리거되지 않아야합니다. 제발 도와 줘, 나는 지금 당장이 일에 매달 렸어.
삭제를 커밋하기 위해 컨텍스트를 저장할 수 있으며'main'은 스토리 보드에서 예상되는 것이 아닌'MainViewController'의 새로운 인스턴스입니다. – vadian