在讀書會中介紹 Core Data Migration 在 swift 3 實作
fb-script
2016年11月3日 星期四
2016年10月26日 星期三
2016年9月11日 星期日
CoreData with swift 2 筆記 - Migration (2) 「半自動」Mapping File and Migration Policy
接上一篇,這次筆記「半自動」模式,計畫如下:
1. 建立一 Use Core Data 專案
2. 建立 Person Entity
3. 在 Person 建立 fullname 欄位
4. 簡單的先在自動產生的 ViewController 中加入一比資料,以空格分開 firstname 和 lastname
5. 首次執行並確定資料新增
6. 刪除新增資料的程式碼
7. 建立一新版本的 Data Model:Model2
8. 在 Model2 中將 fullname 欄位刪除,並加入 firstname 和 lastname 欄位
9. 建立 Model1 到 Model2 的 Mapping File
10. 建立 fullname 欄位的 Migration Policy File:將 fullname 依照空格拆為 firstname 及 lastname
11. 將 Migration Policy 設定給 Mapping File
12. 設定 options 啟動 NSMigratePersistentStoresAutomaticallyOption
13. 第二次執行並確認 .sqlite schema 和資料有成功變更
2016年9月9日 星期五
CoreData with swift 2 筆記 - Migration (1) 「全自動」lightweight migration
承接上一篇,當 NSPersistentStoreCoordinator 在做 addPersistentStoreWithType 時,會將 .sqlite 檔和 data model 做 ORM 的動作,可是如果該 .sqlite 內資料庫的資料和 data model 對不上的時候會怎樣?這種情況會發生在裝置已經安裝過 APP 了,而專案的 data model 有做了變更,使得 data model 和 .sqlite 的內容對不上,此時在預設自動產生的程式碼中,就會將錯誤列出來並呼叫 abort() 將 APP 結束
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
} catch {
// Report any error we got.
// ...
abort()
}
不解決了話, APP 要馬就是不能更新 data model,要馬就是瘋狂的 Crash…