第八章 Core Data by tutorials 笔记(三).pdfVIP

  • 2
  • 0
  • 约7.25千字
  • 约 5页
  • 2017-06-07 发布于湖北
  • 举报
Core Data by tutorials 笔记 (三) 原⽂出处: http://chengway .in/post/ji-zhu/core-data-by-tutorials-bi-ji-san 今天继续来学习Raywenderlich家 《Core Data by Tutorials》的第五章,本章将会聚焦 在NSFetchedResultsController Chapter 5: NSFetchedResultsController 作者在开篇就提 了NS etchedResultsController虽然是⼀个controller ,但是他并不是 ⼀个view controller ,因为他没有view 。 按本章的⽬录梳理⼀下 ⼀、Introducing the World Cup app 本章要完成⼀个World Cup App ,作者提供了⼀个基本的Start Project ,快速浏览⼀ 下,原始数据保存在seed .j son⽂件中。 ⼆、It all begins with a fetch request NSFetchedResultsController⼤概是可以看做对“NS etchRequest获取结果”这⼀过程的 ⼀种封装。话不多说,直接上代码: //1 let fetchRequest = NSFetchRequest(entityName: Team) //2 fetchedResultsCont olle = NSFetchedResultsCont olle (fetchRequest managedObjectContext: co sectionNameKeyPath: nil, cacheName: nil) //3 va e o : NSE o ? = nil if (!fetchedResultsCont olle .pe fo mFetch(e o )) { p intln(E o : \(e o ?.localizedDesc iption)) } 前⾯介绍过,NS etchRequest是可以⾼度定制化的,包括sort descriptors、predicates等 注意⼀下NS etchedResultsController初始化需要的两个必要参数fetchRequest和 context ,第3步由之前的context来perform etch改为NS etchedResultsController来 perform etch ,可以看做是NS etchedResultsController接管了context所做的⼯作,当然 NS etchedResultsController不仅仅是封装perform etch ,他更重要的使命是负责协调 Core Data和Table View显⽰之间的同步。这样⼀来,你所需要做 ⼯作就只剩下了提 供各种定制好的NS etchRequest给NS etchedResultsController就好了。 除了封装了fetch request之外,NS etchedResultsController 内部有容器存储了fetched 回 来的结果,可以使⽤fetchedObjects属性或objectAtIndexPath⽅法来获取 。下⾯是⼀ 些提供的Data source⽅法: func numbe OfSectionsInTableView(tableView : UITableView) - Int { etu n fetchedResultsCont olle .sections!.count } func tableView(tableView : UITableView , numbe OfRowsInSection secti let sectionInfo = fetchedResultsCont olle .sections![section] etu n sectionInfo.numbe OfObjects } sections 数组包含的对象实现了NS

文档评论(0)

1亿VIP精品文档

相关文档