在讀書會中介紹 Core Data 的概念和 swift 2.2 到 swift 3 中間的一些差異
fb-script
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…
2016年9月3日 星期六
CoreData with swift 2 筆記 - Xcode 自動生成程式碼
- 假設專案名稱叫做
CoreDataBloggerDemo
,在專案創建的時候有勾選Use Core Data
的條件下的解析 - 從 xcode 自動生成的 Core Data 程式碼的
// MARK: - Core Data stack
以下開始
這段程式碼是自動生成在
AppDelegate.swift
中,所以要做存取都要使用此物件來存取,獲得此物件方式為
(UIApplication.sharedApplication().delegate) as! AppDelegate
2016年9月1日 星期四
CoreData with swift 2 筆記 - 創建
CoreData 創建
在新建專案的時候,勾選Use Core Data
的選項
- 有沒有勾選這個選項主要差異有兩點,如果有勾選了話
- 1.在 AppDelegate.swift 中會自動產生
// MARK: - Core Data stack
之後的程式碼,主要是在建立 PersistentStoreCoordinator 和 ManagedObjectContext 物件 - 2.會自動創建
專案名稱.xcdatamodelId
的檔案
2015年11月1日 星期日
iOS CoreBluetooth swift 2 連線客製化藍芽 BLE 模組 - ( 4 ) 範例
終於把專案整理了一下
這邊小解釋一下,其他還有問題就留言我盡量幫忙看看囉
首先我把專案設計成三個 ViewController,分別處理不同的事情,如下圖
在 ViewController1 和 ViewController2 的列表 item 如下兩圖