I have a Playlist entity and Song entity. They have a one to many relationship where one playlist contains many songs. I want to remove the relationship and instead add a new attribute called playlistId of type UUID and assign the playlist Id to it during migration.
I have tried and check all the class names in the Mapping model file but the methods refuse to get called.
Is it because my core data stack is in a custom directory? At the moment in the mapping model, the custom class name is set to “MyAppName.MappingModel”
class DataController {
static let shared = DataController()
let container: NSPersistentContainer!
init() {
container = NSPersistentContainer(name: "Database")
let coreDataDirectory = FileManager.getApplicationSupportDirectory().appendingPathComponent("Database")
let description = NSPersistentStoreDescription(url: coreDataDirectory)
self.container.persistentStoreDescriptions = [description]
if let description = container.persistentStoreDescriptions.first {
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = false
}
container.loadPersistentStores { [weak self] description, error in
guard let self else { return }
if let error = error {
print("Core Data failed to load: (error.localizedDescription)")
fatalError()
}
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
}
}
class MappingModel: NSEntityMigrationPolicy {
override func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws {
//Never called.
print("Running migration")
}
}
extension Song {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Song> {
return NSFetchRequest<Song>(entityName: "Song")
}
@NSManaged public var album: String?
@NSManaged public var artist: String?
@NSManaged public var artwork: String?
@NSManaged public var fileExtension: String?
@NSManaged public var name: String?
}