新人資訊
技術-iOS #11-本地端儲存新選擇:Realm
在 Core Data 中碰釘子後心中暗暗期待救星的來臨,沒想到很快就盼到了,Realm 使用經驗非常愉快,邏輯更直覺,程式更精簡,繼續延伸上個專案 Todo List,這次要完整實作 Categort and Items 一對多資料關聯結構,完整的 CRUD 功能。
看了一下 Realm 官網資料,原來它並不隸屬 apple,而是由鼎鼎大名的 NoSQL 資料庫 MongoDB 發展出來的, 提供各種語言的 SDK,所以學它有機會用到別處,也有可能延伸使用經驗到雲端架構,看到這裡,我迫不及待的把 Core Data 從我腦袋清除乾淨了。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
先安裝 SDK,使用 Swift Package Manager or CocoaPods 都可,這兒說明非常清楚明瞭:[https://www.mongodb.com/docs/realm/sdk/swift/install/]
建立資料庫物件,與一般的 class 幾乎一模一樣,也可以建立兩物件的關聯:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
先實作「新增 Category」物件,在 category view controller 程式開頭先宣告整體變數:
let realm = try! Realm()
var categories: Results<Category>?
程式的觸發點是按下 Add Button 後,生成對話筐 Alert,裡面包含一個 TextField and Action,Action 被觸發時,以 closure 形式撰寫:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
再實作「查詢 Category」,在畫面 viewDidLoad() 呼叫以下,竟然精簡到一行完成:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
然後為了處理 Items 必須牽涉到 Segue 和把 Category 屬性傳到 Items 畫面:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在 TodoListViewController 中設定一個屬性接受 Category,在 didSet() 裏執行查詢 Items 的動作,原本觀念會以為此查詢要傳入 Category 當 filter,其實不用這麼麻煩,因為 selectedCategory 本身就是資料庫物件,所以直接取得其 items 屬性即可,關聯已經在 class 定義裡設定完成囉!超棒的。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在 Items 中就要實作資料庫的 update 功能了,把物件的修改包在 realm.write 裏面即可:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
最後還有刪除功能;因為點選 Cell 已經作為切換「標示完成」的功能,所以引進 swipe 操作方式,這種因應觸控介面而誕生的方法,目前已經逐漸成為大家習慣的標準配備了。引進第三方套件 [https://github.com/SwipeCellKit/SwipeCellKit],照系統建議我用 cocoapods 安裝。
因為兩個頁面 Category and Items 都需要此功能,嘗試建立一個共用的父類別,把套件說明的必要動作放進去,而實際執行刪除動作時,會因子類別而不同,所以建立一個空的 updateModel() 方法帶子代覆寫。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
categoryViewController
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
TodoListViewController子代除了覆寫 updateModel() 以外,以下部分需要先呼叫 super.tableView() 方法後接續各自行為:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
最後 Search 部分,因為資料庫物件已經存在程式執行環境,所以執行一個物件串列的 filter 操作,就不需重新查詢資料庫了:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
執行狀況如下:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
By Newman Chen 2022/9/2