2014-12-04 3 views
2

NSFetchedResultController에 문제가 있습니다. coreData : actualDate 및 shortDate라는 두 값을 저장하는 버튼이 있습니다. actualDate는 NSDate 변수 (예 : 2014-12-04 08:35:59 +0000)이고 shortDate는 NSDateFormatter (예 : 04 dec)를 통해 actualDate에서 생성 된 String입니다.날짜순 NSFetchedResultController 섹션 정렬 재 배열

아래이 버튼은 모든 항목을 표시해야하는 tableView로, 날짜별로 내림차순으로 정렬되며 각 날짜의 섹션 이름으로 shortDate (예 : 04 dec)로 정렬됩니다. 이 응용 프로그램을 실행하는 동안 내 현재 코드를 잘 작동합니다.

앱을 종료하고 다시 시작하면 문제가 발생합니다. 항목은 여전히 ​​올바른 순서이지만 섹션 이름은 예를 들어 11 월 30 일이 4 월 12 일 이전에 표시되도록 다시 정렬됩니다. 나는 정말로 문제가 무엇인지 어떻게 해결할 지 모른다.

도움을 주시면 감사하겠습니다. 나는 이것이 오래된 질문 알고

// 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() 
} 
+0

날짜가 어떻게 핵심 데이터에 저장됩니까? – Garret

+0

시도해 보셨습니까? http://stackoverflow.com/questions/4418703/a-nsfetchedresultscontroller-with-date-as-sectionnamekeypath – Garret

+0

이 문제에 대한 해결책이 있습니까? 위의 링크는 date 객체에 의해 sectionKey에 대한 방법을 제안하지만, 앱을 다시 시작할 때 섹션 헤더가 순서가 잘못 표시된다는 문제를 건드리지 않습니다. – JLust

답변

0

하지만 같은 문제에 직면하고 어떻게 든 내가 cacheName을 설정하는 대신 그것을 nil을 수 있도록함으로써이 문제를 해결하기 위해 관리 :

여기 NSFetchedResultController 내 코드입니다.

저는 같은 문제에 직면 해있는 모든 사람들을 위해이 오래된 질문에 답했습니다.