iOS Persistence and Core Data Lesson 5 - Udacity的筆記
筆記目錄:
1.保存數據的基礎知識
2.如何使用NSCoder/NSKeyedArchiver保存數據
3.Core Data的使用
4.Core Data的構建
5.Core Data的框架(所在章節)
各文章的介紹:我的女朋友CoreData和她閨蜜們
Smart Architecture w/Core Data
前言
終于寫到iOS Persistence and Core Data的最后一篇筆記了。試著跟別人解釋這樣寫筆記能令自己對一些知識看得透徹一點。因為不斷假設:"假如別人這么問,我應該怎么答呢?"于是不斷問自己問題,不斷解答,強迫自己思考
把最后的感言寫在這里,那么剩下的全都是技術
最后
終于把這文章寫完了,第一次通過一問一答的形式,進行做這個筆記,自我感覺不錯。以前,看數學書時,笛卡爾就提過“將大問題分成若干個小問題,再一個一個解決”
當Model(數據)更新的時候,View Controller如何知道Model更新了?
Core Data 推薦以下兩種方式:
1.Notification Center
2.NSFetchResultsController
這篇文章就是介紹如何使用的NSFetchResultsController
NSFetchResultsController
扮演的是什么角色?
如圖,MovieDB
是服務器,FetchController
就是通知Master Controller
數據是否有更新 ,然后將數據從Core Data的Context
傳遞給Master Controller
,
對原有的Project怎么替換為NSFetchResultsController
?
Project要利用NSFetchResultsController
兩個特性:獲取與通知。則產生如下兩個問題:
1.如何獲取Context里的數據
2.NSFetchResultsController
如何通知Controller
先來解決第一個問題
如何獲取Context里的數據
如上面的圖,問題拆分為兩個:
1.
NSFetchResultsController
怎么從Context當中獲取數據
2.Controller如何從NSFetchedResultsController
中獲取數據?
獲取Core Data保存的數據
原有的Project從Context直接取出數據,而這里則是通過NSFetchResultsController
獲取數據.
那么NSFetchResultsController
怎么從Context當中獲取數據?
1.設置NSFetchRequest
2.設置NSFetchedResultsController
代碼如下:
lazy var fetchedResultsController: NSFetchedResultsController = {
// Create the fetch request
let fetchRequest = NSFetchRequest(entityName: "Event")
// Add a sort descriptor.
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timeStamp", ascending: false)]
// Create the Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.sharedContext, sectionNameKeyPath: nil, cacheName: nil)
// Return the fetched results controller. It will be the value of the lazy variable
return fetchedResultsController
} ()
然后只需在恰合的地方,譬如viewDidLoad,使用以下代碼就能讓NSFetchedResultsController
獲取到數據。
do {
try fetchedResultsController.performFetch()
} catch {
print("Unresolved error \\\\(error)")
abort()
}
接下來Controller如何從NSFetchedResultsController
中獲取數據?
Object
let event = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Event
Objects數量
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section]
return sectionInfo.numberOfObjects
}
NSFetchResultsController
如何通知Controller
?
通過delegate,其中delegate method有一下四個本lesson用到的:英文就不翻譯了,因為感覺更符合
"I am about to tell you about some changes"
controllerWillChangeContent(_:)
"An object changed (an Event was inserted, removed, or altered)"
controller(_:didChangeObject:atIndexPath:forChangeType:newIndexPath:)
"Your table’s sections should change"
controller(_:didChangeSection:atIndex:forChangeType:)
"I am done telling you about changes for now"
controllerDidChangeContent(_:)
那么在代碼中使用是為了刷新tableView,使view重新繪制,所以代碼如下:
func controllerWillChangeContent(controller: NSFetchedResultsController) {
// This invocation prepares the table to recieve a number of changes. It will store them up
// until it receives endUpdates(), and then perform them all at once.
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
// Our project does not use sections. So we can ignore these invocations.
}
//
// This is the most important method. It adds and removes rows in the table, in response to changes in the data.
//
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)
default:
return
}
}
// When endUpdates() is invoked, the table makes the changes visible.
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
如果不想將Context里所有數據都取出,而是要其中的符合條件,該如何做?
例如Context里有如圖的
每個Person都有專屬的Moive,而現在我們想要某一Person的Moive,怎么辦?
我們可以使用Predicate
let fetchRequest = NSFetchRequest(entityName:"Movie")
//過濾,返回Movie里actor = self.actor的Movie
fetchRequest.predicate = NSPredicate(format:"actor == %@",self.actor)
具體用法可以參考以下:
Predicate Programming Guide
-------END-------
---And Thank U----