按日期排序的NSFetchedResultController部分将重新排列

时间:2014-12-04 08:46:27

标签: ios date swift nsfetchedresultscontroller sections

我遇到NSFetchedResultController问题。我有一个按钮,可以将两个值保存到Core Data:actualDate和shortDate。 actualDate是一个NSDate变量(例如2014-12-04 08:35:59 +0000),shortDate是一个String,通过NSDateFormatter(例如04 dec)从actualDate创建。

在此按钮下方是一个tableView,它应显示所有条目,按日期降序排序,shortDate作为每天的部分名称(例如,04 dec)。这适用于我当前的代码WHILE RUNNING应用程序。

关闭应用并再次启动时会出现问题。这些条目仍然是正确的顺序,但是部分名称被重新排列,例如,在12月4日之前显示的是11月30日。我真的不知道问题是什么或如何解决它。

感谢任何帮助!

这是我的NSFetchedResultController代码:

// MARK: - Fetched results controller

var fetchedResultsController: NSFetchedResultsController {
    if _fetchedResultsController != nil {

        return _fetchedResultsController!
    }

    let fetchRequest = NSFetchRequest()
    // Edit the entity name as appropriate.
    let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: self.managedObjectContext!)
    fetchRequest.entity = entity

    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20

    // Edit the sort key as appropriate.
    let sortDescriptor = NSSortDescriptor(key: "actualDate", ascending: false)
    let sortDescriptor2 = NSSortDescriptor(key: "shortDate", ascending: false)

    fetchRequest.sortDescriptors = [sortDescriptor, sortDescriptor2]


    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "shortDate", cacheName: nil)
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    var error: NSError? = nil
    if !_fetchedResultsController!.performFetch(&error) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        //println("Unresolved error \(error), \(error.userInfo)")
        abort()
    }

    return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil

func controllerWillChangeContent(controller: NSFetchedResultsController) {
    self.tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
    switch type {
    case .Insert:
        self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
    case .Delete:
        self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
    default:
        return
    }
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
 /*   case .Update:
        self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)*/
    case .Move:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
    default:
        return
    }
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    self.tableView.endUpdates()
}

1 个答案:

答案 0 :(得分:0)

我知道这是一个老问题,但我遇到了同样的问题,不知怎的,我设法通过设置cacheName而不是让它nil来解决这个问题。

我为每个面临同样问题的人回答了这个老问题。