[ad_1]
I’ve a Core Knowledge knowledge mannequin with a number of mannequin variations (saved to permit migrations). My entities have the codegen setting set to Handbook / none, and I’ve corresponding courses in my codebase the place I hook into sure strategies like validateForUpdate, awakeFromInsert, and so on:
@objc(E-book)
class E-book: NSManagedObject {
@NSManaged var title: String
override func validateForUpdate() throws {
strive tremendous.validateForUpdate()
strive customValidation()
}
}
Throughout migration of an outdated retailer, it’s needed for me to load the outdated retailer utilizing the outdated mannequin model and carry out some updates to managed objects. Nevertheless, the validation from the NSManagedObject class nonetheless runs, though this was solely designed to run in opposition to the most recent model. It might seek advice from attributes that do not exist within the outdated model, giving me unrecognized selector errors.
An instance of the code I am utilizing to load a retailer with the outdated mannequin is:
// book_5 is the title of the older mannequin model
let mannequin = NSManagedObjectModel(contentsOf: Bundle.important.url(forResource: "books_5", withExtension: "momd")!)!
let container = NSPersistentContainer(title: "TestModel", managedObjectModel: mannequin)
let description = NSPersistentStoreDescription(url: storeLocation)
container.persistentStoreDescriptions = [description]
container.loadPersistentStores { _, _ in
let entity = mannequin.entitiesByName["Book"]!
let newBook = NSManagedObject(entity: entity, insertInto: container.viewContext)
newBook.setValue("Take a look at E-book", forKey: "title")
strive! container.viewContext.save()
}
When inserting the thing into the context, the awakeFromInsert technique is fired, triggering an error as a result of it refers to attributes that solely exist within the latest mannequin model.
Is there a solution to workaround this?
[ad_2]
