LoginSignup
0

More than 3 years have passed since last update.

NSFetchedResultsControllerの使い方

Posted at

NSFetchedResultsControllerを使う際注意点のまとめ

1.1つ以上のsort descriptorが必要

let nameSort =  NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [cateSort, nameSort]

2.Grouping results into sectionsの際、sectionNameKeyPathの設定も必要し、sectionNameKeyPathのKeyを1番のsort descriptorとして追加することも必要

fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
                           managedObjectContext: coreDataStack.context, 
                           sectionNameKeyPath: "category",cacheName: nil)
let cateSort = NSSortDescriptor(key: "category", ascending: true)
let nameSort =  NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [cateSort, nameSort]

3.sort descriptorの追加される順でsortする。

例えば

let zoneSort = NSSortDescriptor(key: "qualifyingZone", ascending: true)
    let scoreSort = NSSortDescriptor(key: "wins", ascending: false)
    let nameSort =  NSSortDescriptor(key: "teamName", ascending: true)
    fetchRequest.sortDescriptors = [zoneSort, scoreSort, nameSort]

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0