iOS Core Data 升級 – Lightweight Migration

Photo by Mark Sarmiento on Unsplash
Photo by Mark Sarmiento on Unsplash
Core Data 在 iOS 開發中是儲存資料的首選。當 APP 已經上線了之後,下一版要更動 Attribute,我們就需要處理 Core Data 的升級。所幸 Xcode 已經有提供 Core Data 升級的管理機制,就是 Core Data Lightweight Migration。

Core Data 在 iOS 開發中是儲存資料的首選。當 App 已經上線了之後,下一版要更動 Attribute,我們就需要處理 Core Data 的升級。所幸 Xcode 已經有提供 Core Data 升級的管理機制,就是 Core Data Lightweight Migration。本章會介紹如何使用 Core Data Lightweight Migration。

第一節,我們會先建立一個新的專案,並且新增一個 Entity 叫 Person。如果你已經知道如何建立一個 Core Data 的專案,請直接閱讀第二節。

第二節,我們會對這個 Person Entity 新增一個 Attribute。

完整的程式碼可以在 下載。

建立新的專案

建立一個 Xcode 專案叫 CoreDataLightweightMigrationExample。在 CoreDataLightweightMigrationExample.xcdatamodeld 中新增一個 Entity 叫 Person,並且新增兩個 Attributes 叫 name 和 email,Type 都是 String

Person entity
Person entity

升級 Core Data Entity

現在我們要對 Person 新增一個 Attribute 叫 phone,變更後的 Person 就是第二版。當用戶已經安裝第一版的 APP,然後升級安裝第二版 APP 後,我們希望第二版 APP 可以偵測到 Person 已經有變更,並且可以自動將第一版的 Person 升級到第二版的 Person,並且不破壞已在的 Person 資料。

對 CoreDataLightweightMigrationExample.xcdatamodeld 新增一個新的本版,叫 CoreDataLightweightMigrationExample 2.xcdatamodeld。有綠色勾勾的是目前 Xcode 選用的版本。我們在 File Inspector 中的 Model Version,選用新的版本。

Add Model Version to Core Data
新增新的版本,並在 Model Version 中選用新版。

選取 CoreDataLightweightMigrationExample 2.xcdatamodeld,在 Person 裡新增 Attribute 叫 phone 。

Person entity v2
Person entity v2

然後在 Person+CoreDataProperties.swift 中加入 phone 的宣告。

extension Person {
    @nonobjc public class func fetchRequest() -> NSFetchRequest<Person> {
        return NSFetchRequest<Person>(entityName: "Person")
    }

    @NSManaged public var name: String?
    @NSManaged public var email: String?
    @NSManaged public var phone: String?
}

接下來,我們要告訴 Core Data 在載入 Person 的時候,要自動做版本升級。這需要將兩個變數設定為 true:

  • shouldMigrateStoreAutomatically:告訴 Core Data 自動做版本升級 (Migration)
  • shouldInferMappingModelAutomatically:告訴 Core Data 自動推導 mapping model。
persistentContainer = NSPersistentContainer(name: "CoreDataLightweightMigrationExam")
let description = persistentContainer.persistentStoreDescriptions[0] 
description.shouldMigrateStoreAutomatically = true 
description.shouldInferMappingModelAutomatically = true 
persistentContainer.persistentStoreDescriptions = [description]
persistentContainer.loadPersistentStores { (storeDescription, error) in
    if let error = error {
        fatalError("Error loading store \(storeDescription), \(error)")
    }
}
managedObjectContext = persistentContainer.newBackgroundContext()

結語

Core Data 提供的 Lightweight Migration 機制是非常強大的。在 APP 持續地開發時,新的需求常常會需要增加新的 Attribute,或是變更 Attribute 的 Type。Core Data 管理這些變更,讓開發者可以專注在邏輯上的開發。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

You May Also Like
Photo by Alex Alvarez on Unsplash
Read More

Dispatch Queue 教學

GCD 提供有效率的並行處理,讓我們不需要直接管理多執行緒。它的 Dispatch Queues 可以循序地(serially)或是並行地(concurrently)執行任務。我們只需要將要並行的程式當作任務提交到 dispatch queues 就可以了。
Read More
Photo by Florinel Gorgan on Unsplash
Read More

如何製作一個 XCFramework

XCFramework 讓你可以將 iPhone、iPhone 模擬器等多的不同平台的二進位碼打包到一個可發佈的 .xcframework 檔。你只需要為你的 Framework 產生出一個 .xcframework 檔,就可以支援多種平台。
Read More
Photo by Fabian Gieske on Unsplash
Read More

SwiftUI @State & @Binding 教學

SwiftUI 推出了兩個 Property Wrapper – @State and @Binding。利用它們可以達到變數的 Two-way Binding 功能。也就是當變數的值改變時,它會重新被顯示。本章藉由製作一個 Custom View 來展示如何使用 @State 和 @Binding。
Read More