0

저는 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 
} 

및 다시 합계 시간을 예상대로 뺍니다. 그래서 그것을 닫을 때까지 삭제가 트리거되지 않아야합니다. 제발 도와 줘, 나는 지금 당장이 일에 매달 렸어.

+1

삭제를 커밋하기 위해 컨텍스트를 저장할 수 있으며'main'은 스토리 보드에서 예상되는 것이 아닌'MainViewController'의 새로운 인스턴스입니다. – vadian

답변

0

@vadian 노트와 마찬가지로, 핵심 문제는 main이 화면에 표시된 것과 완전히 독립적이라는 것입니다. 뷰 컨트롤러를 만들고 수정 한 다음 버리십시오.

귀하의 MainViewController은 핵심 데이터를 준수하고 데이터가 변경 될 때 자체를 수정해야합니다. 그러면 예상대로 업데이트됩니다.