Datacore Startper core是什么意思思

CoreData之NSFetchedResultsController - 简书
CoreData之NSFetchedResultsController
看了一段时间的CoreData Codes,随着涉猎越多,便越发觉得CoreData设计之精妙。今日在此分享CoreData之NSFetchedResultsController的妙用。
NSFetchedResultsController的设计初衷是为了在UITableview中高效地管理那些从Core Data中fetch得到的数据。UITableView使用fetch request得到的数据来配置table cell,当Core Data中的数据产生变化时,NSFetchedResultsController能够有效地对得到的结果进行分析,并对UITableView进行调整。
当Core Data中数据产生变化,使得UITableView中展示的内容需要更新时,NSFetchedResultsControllerDelegate能够帮助开发者大大减少代码量,节省不少时间。
TapList-Start
下面,就一个简单的例子,来看下NSFetchedResultsController的强大力量。
下载并打开工程,首先来看TapList的Data Model:
data model
该Model中只有一个Entity,名为Item。每个item有3个属性,其中name代表名字,score是代表分数,image代表这个item的图片。
table view中的一个cell表示一个item,cell布局如下:
table cell
table view把Core Data中存储的所有item按score从高到低的顺序排列。当点击某个cell的时候,该cell对应的item的score就加1,table view要根据最新的数据对cell进行实时更新。
以上就是TapList这个小应用的主体了。如果不用NSFetchedResultsController,该应用将在Core Data中数据有变化的时候调用reloadTable方法来实时更新显示界面。接下来,我将在此基础上介绍引入NSFetchedResultsController的实现方式,你可以看到NSFetchedResultsController带来的便利。
TapList-NSFetchedResultsController
在原代码中将updateTableView方法和tableData全部删去。虽然这会导致Xcode报出不少bug出来,但没关系,慢慢来。
1.在ViewController中添加一个NSFetchedResultsController属性:
var fetchedResultsController: NSFetchedResultsController!
2.在viewDidLoad中,修改代码如下:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = CGFloat(76)
self.tableView.rowHeight = CGFloat(76)
let fetchRequest = NSFetchRequest(entityName: "Item")
let sortDescriptor = NSSortDescriptor(key: "score", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.context, sectionNameKeyPath: nil, cacheName: nil)
try fetchedResultsController.performFetch()
} catch let error as NSError {
print("Error: \(error.userInfo)")
首先构建一个fetchRequest,这时必须指定fetchRequest的sortDescriptors,因为table view必须按某个顺序来排列一个个cell。
接下来,NSFetchedResultsController使用上一步构造的fetchRequest来初始化自己。
紧接着,fetchedResultsController调用performFetch方法来从Core Data中取出数据。调用这个方法后,不需要reloadTable了,因为fetchedResultsController会根据你所定义的UITableViewDataSource相关方法,来替你配置cell并更新UI。
3.在UITableViewDataSource相关方法中,实现如下:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -& Int {
return fetchedResultsController.fetchedObjects!.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -& UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath) as! ItemCell
cell.item = fetchedResultsController.objectAtIndexPath(indexPath) as? Item
return cell
其中,fetchedResultsController.fetchedObjects记录了所有fetch得到的item,能根据其数目确定cell的数量,并配置相应cell。
4.在didSelectRowAtIndexPath中更新如下:
let item = fetchedResultsController.objectAtIndexPath(indexPath) as! Item
5.extension ViewController,使其conform NSFetchedResultsControllerDelegate 协议:
extension ViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Automatic)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
case .Update:
let cell = tableView.cellForRowAtIndexPath(indexPath!) as! ItemCell
cell.item = fetchedResultsController.objectAtIndexPath(indexPath!) as? Item
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Automatic)
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
这部分代码是使NSFetchedResultsController发挥作用的关键。主要过程分为以下3步:1.在controller的content即将改变的时候,允许tableView进行更新。2.对object的各种改变进行相应处理。其中anObject是发生变化的物体,indexPath指向改变前,newIndexPath指向改变后。3.在controller的content改变即将完成时,使tableView结束更新。
6.最后,别忘了在viewDidLoad设置fetchedResultsController的delegate:
fetchedResultsController.delegate = self
7.重新运行该app,会发现该app不仅能正常运行,记录并实时更新item的信息,而且多了一些动画效果。这也是NSFetchedResultsController带来的Bonus之一。
最终Demo已经上传到,希望这篇文章对你有所帮助^_^。
浙大土著。
对于身边的女博士十分敬仰。
爱打网球的小吃货。

我要回帖

更多关于 coretime是什么意思 的文章

 

随机推荐