gpt4 book ai didi

ios - NSFetchedResultController 按日期排序部分得到重新排列

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:16 25 4
gpt4 key购买 nike

我对 NSFetchedResultController 有疑问。我有一个按钮可以将两个值保存到核心数据:actualDate 和 shortDate。 actualDate 是一个 NSDate 变量(例如 2014-12-04 08:35:59 +0000),而 shortDate 是一个字符串,通过 NSDateFormatter 从 actualDate 创建(例如 12 月 4 日)。

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

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

感谢任何帮助!

这是我的 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()
}

最佳答案

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

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

关于ios - NSFetchedResultController 按日期排序部分得到重新排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27289633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com