如何更改 SwiftData 中的实体名称

问题描述 投票:0回答:1

我正在 coredata 项目中使用 SwiftData。

我有核心数据模型(Event、EventDate、Lecture),并使用模型宏转换为 swiftdata 模型。

enum V3Schema: VersionedSchema {
    static var models: [any PersistentModel.Type] = [Event.self, Lecture.self]
    
    static var versionIdentifier = Schema.Version(3, 5, 2)

    @Model public class Event {
        var date: Date?
        var memo: String?
        var status: Bool? = false
        var title: String?
        var lecture: Lecture?

        public init() { }
        
    }

    @Model public class Lecture {
        var color: String?
        var room: String?
        var title: String?
        @Relationship(deleteRule: .cascade, inverse: \Event.lecture) var events: [Event]?
        @Relationship(deleteRule: .cascade, inverse: \LectureTime.lecture) var times: [LectureTime]?
        

        public init() { }
        
    }
}

我想更改其他版本中的实体名称,如下面的代码。

enum V4Schema: VersionedSchema {
    static var models: [any PersistentModel.Type] = [LectureModel.self, EventModel.self]
    
    static var versionIdentifier = Schema.Version(4, 0, 0)

    @Model class EventModel {
        var title: String = ""
        var status: EventStatus = EventStatus.incomplete
        var memo: String?
        var deadline: Deadline?
        var lecture: LectureModel?
        
        struct Deadline: Codable, Comparable {
            static func < (lhs: V4Schema.EventModel.Deadline, rhs: V4Schema.EventModel.Deadline) -> Bool {
                lhs.startAt < rhs.startAt
            }
            
            var startAt: Date
            var endAt: Date
            var isTimeRequired: Bool
        }
        
        enum EventStatus: Int, Codable {
            case incomplete = 0
            case complete = 1
        }
        
        init(eventID: String, title: String, memo: String? = nil, date: Deadline? = nil, status: EventStatus = .incomplete) {
            self.eventID = eventID
            self.title = title
            self.memo = memo
            self.deadline = date
            self.date = deadline?.startAt
            self.status = status
        }
        
    }
}

我为变更模型编写了迁移计划,但迁移计划没有被调用。 (我添加了日志来检查迁移是否正常,但日志没有出现。)

enum ModelMigrationPlan: SchemaMigrationPlan {
    static var schemas: [any VersionedSchema.Type] = [V3Schema.self, V4Schema.self]
    static var stages: [MigrationStage] = [migrateV3ToV4]
    
    static let migrateV3ToV4 = MigrationStage.custom(fromVersion: V3Schema.self,
                                                     toVersion: V4Schema.self,
                                                     willMigrate: { _ in 
    Log.debug(message: "Migration Start")
    \\ create new EventModel in V4Schema using Event in V3Schema.
},
                                                     didMigrate: { _ in Log.debug(message: "Migration Complete") })
    
}

问题1。如何更改 SwiftData 中的实体名称? 问题2。如何在SwiftData中使用重量级迁移?

migration swift-data
1个回答
0
投票

看看这里:https://www.hackingwithswift.com/quick-start/swiftdata/how-to-create-a-complex-migration-using-versionedschema

如果您想更改实体名称,(还没有?)没有一种简单的方法来迁移数据库,这意味着在幕后更改数据库表名称。

但是,如果您遵循链接文章中描述的方法,当然是可能的。

© www.soinside.com 2019 - 2024. All rights reserved.