Core Data的框架

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 DataContext傳遞給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----

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,981評論 19 139
  • 在這篇文章中,我們將建立一個小型但卻全面支持 Core Data 的應用。此應用允許你創建嵌套的列表;每個列表的 ...
    評評分分閱讀 590評論 0 5
  • 晨 地平線還泛著微光。一簇簇人聚集海邊 或是佯裝散步 或是擺弄鏡頭 像在等待著什么。終于某一時刻 集體掀起黑幕 偷...
    月亭閱讀 172評論 0 0
  • 楠子今天說: 領導人品差,打擊報復,即將離職還開部門會議把我平時工作中無關緊要的小失誤亮出來,故意刁難,說態度和底...
    白小白女性生涯規劃閱讀 1,334評論 0 3
  • 愛美之心人皆有之!隨著社會和經濟發展,人們對美的追求愈加強烈,催生了繁榮的美容護膚產業,層出不窮的美容方法,給消費...
    hbinglan閱讀 158評論 0 0