Core Data by tutorials 筆記(一)

2018-02-24 15:54 更新

原文出處: http://chengway.in/post/ji-zhu/core-data-by-tutorials-bi-ji

最近花了半個(gè)月讀完了Raywenderlich家的《Core Data by Tutorials》,接下來(lái)幾天就做個(gè)回顧,筆記并不是對(duì)原書(shū)的簡(jiǎn)單翻譯,算是對(duì)整個(gè)知識(shí)脈絡(luò)的一個(gè)整理的過(guò)程吧:)

Update:?上周去搞Sketch了,工具算是基本入門(mén)了,抄了幾個(gè)圖標(biāo),畫(huà)了幾個(gè)圖,以后會(huì)再聊聊。好了,進(jìn)入正題吧,今天打算介紹前三章,也是CoreData的最基礎(chǔ)的部分。***

Chapter 1:Your First Core Data App

第一章比較簡(jiǎn)單,主要是帶你熟悉CoreData的用法,并用CoreData創(chuàng)建了一個(gè)簡(jiǎn)單的List APP,學(xué)完這章你能加滿如下技能點(diǎn):

  • model data you want to store in Core Data using Xcode’s model editor;
  • add new records to Core Data;
  • fetch a set of records from Core Data;
  • display the fetched results to the user in a table view.

第一章按順序大致如下:

一、Getting started

作者提供了一個(gè)Start Project,打開(kāi)其實(shí)就是一個(gè)UITableViewController,你需要做的就是增加一個(gè)addName方法,這里使用了UIAlertController這個(gè)iOS 8新添加的方法來(lái)做數(shù)據(jù)的添加邏輯,使用起來(lái)也很優(yōu)雅。

二、Modeling your data

這個(gè)主要是根據(jù)你的數(shù)據(jù)模型創(chuàng)建對(duì)應(yīng)的managed object model,主要操作也在.xcdatamodeld中進(jìn)行,全GUI操作,沒(méi)什么難度。

You can think of a Core Data entity as a class “definition” and the managed object as an instance of that class.
這里作者用了面向?qū)ο蟮乃枷胱隽祟?lèi)比,雖然不是很準(zhǔn)確,但也方便初學(xué)者理解。

三、Saving to Core Data

這里要注意到NSManagedObject的“對(duì)象”其實(shí)是遵循“KVC”的,NSManagedObject對(duì)象的值改變后,在ManagedContext中進(jìn)行save操作。

四、Fetching from Core Data

創(chuàng)建一個(gè)NSFetchRequest對(duì)象,設(shè)置好各種條件,依舊是交給ManagedContext對(duì)象去執(zhí)行。下面是作者描述了兩種fetch失敗的情況

If there are no objects that match the fetch request’s criteria, the method returns an optional value containing an empty array.
If an error occurred during the fetch, the method returns an optional value that contains nil. If this happens, you can inspect the NSError and respond appropriately.

第一章到此結(jié)束:)


Chapter 2:NSManagedObject Subclasses

NSManagedObject上一章我們提到過(guò),要存取屬性,只能用KVC,使用起來(lái)既不安全也不符合面向?qū)ο蟮脑瓌t,所以我們這章要生成他的子類(lèi),來(lái)創(chuàng)建我們自己的屬性,這樣就又能愉快地使用"."語(yǔ)法啦。

第二章的大致順序如下:

一、Getting started

作者提供了一個(gè)“挑選領(lǐng)結(jié)”的Start Project,領(lǐng)結(jié)數(shù)據(jù)存放在plist文件中,同樣是一個(gè)很簡(jiǎn)單的App。

二、Modeling your data

打開(kāi)xcdatamodeld文件進(jìn)行編輯,添加屬性。這里主要看一下Binary DataTransformable兩種類(lèi)型:

  • Binary Data這里將image對(duì)象保存成了二進(jìn)制文件,當(dāng)然任何可以序列化的對(duì)象其實(shí)都可以保存成二進(jìn)制。這里作者強(qiáng)調(diào)了性能問(wèn)題,如果將一個(gè)很大的二進(jìn)制保存到SQLite數(shù)據(jù)庫(kù)中很可能會(huì)產(chǎn)生卡頓等問(wèn)題。當(dāng)然,幸運(yùn)的是Core Data提供了一種叫Allows External Storage的解決方式,他只對(duì)binary data的屬性類(lèi)型有效,你只需要簡(jiǎn)單的開(kāi)啟他就好了。

    When you enable Allows External Storage, Core Data heuristically decides on a per-value basis if it should save the data directly in the database or store a URI that points to a separate file.

  • Transformable除了一些基本屬性,如果一個(gè)對(duì)象遵循NSCoding Protocol,那么這個(gè)對(duì)象是可以選擇使用transformable類(lèi)型的,作者這里使用的是UIColor類(lèi)型,遵循NSSecureCoding協(xié)議,而該協(xié)議又繼承自NSCoding,所以設(shè)為T(mén)ransformable是OK的。自定義的類(lèi)想要設(shè)置為Transformable,那么你首先要實(shí)現(xiàn)NSCoding protocol

三、Managed object subclasses

在Xcode中選擇Editor\Create NSManagedObject Subclass創(chuàng)建managedObject對(duì)象的子類(lèi),這里注意下Map Model中的attribute type與實(shí)際子類(lèi)對(duì)象中的屬性的對(duì)應(yīng)關(guān)系就好了

  • String maps to String
  • Integer 16/32/64, Float, Double and Boolean map to NSNumber
  • Decimal maps to NSDecimalNumber
  • Date maps to NSDate
  • Binary data maps to NSData
  • Transformable maps to AnyObject

當(dāng)然如果你想保留Map Model中的原始基本類(lèi)型,那么在創(chuàng)建NSManagedObject Subclass時(shí)要勾選Use scalar properties for primitive data types

Similar to @dynamic in Objective-C, the @NSManaged attribute informs the Swift compiler that the backing store and implementation of a property will be provided at runtime instead of at compile time.
這里注意下兩種語(yǔ)言的一點(diǎn)區(qū)別

創(chuàng)建完子類(lèi)還要記得一點(diǎn)就是在Model中的Attributes inspectorclass name設(shè)為你新生成的子類(lèi)路徑,完成這一步主要是為了在runtime時(shí)將managed object subclass與Model中的entity鏈接起來(lái)。(有點(diǎn)類(lèi)似與在SB中創(chuàng)建一個(gè)VC,并設(shè)置他的custom class)。

四、Propagating a managed context

因?yàn)閄code默認(rèn)的CoreData模板會(huì)將context在AppDelegate中創(chuàng)建,所以,就會(huì)有這種獲取context的做法:

 let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate 

但這種做法還是不推薦使用,較好的方法是能夠在對(duì)象的的初始化中將context作為參數(shù)進(jìn)行傳遞。
接下來(lái),作者從plist中將數(shù)據(jù)通過(guò)子類(lèi)對(duì)象導(dǎo)入到core data中,再對(duì)界面做了些操作,這都比較簡(jiǎn)單。

五、Data validation in Core Data

數(shù)據(jù)校驗(yàn)這里可以在data model inspector 中設(shè)置最大最小值,如果出錯(cuò)會(huì)在context save的時(shí)候?qū)㈠e(cuò)誤信息傳給error參數(shù)

第二章到此結(jié)束:)


Chapter 3:The Core Data Stack

Core Data要正常運(yùn)行,需要幾個(gè)組件共同協(xié)作來(lái)完成,這些組件包括:

  • NSManagedObjectModel
  • NSPersistentStore
  • NSPersistentStoreCoordinator
  • NSManagedObjectContext

這些組件共同構(gòu)成了Core Data Stack,組件間的關(guān)系我覺(jué)得用蘋(píng)果官方提供的一張圖來(lái)展示最好不過(guò)了

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)