I have an app that relies on Core Data for persistence. The app is using MVVM architecture in order to facilitate testing The dev environment is a MacBook Pro (M1 Max) running MacOS 12.4 and Xcode 14 beta 2
So far everything has been working well until I attempted to add a new entity to my Model that had a relationship (many-to-one) to another entity. Attempting to instantiate one of these entities resulted in a NSUnknownKeyException associated with the property that defined the relationship so I deleted this relationship from the model and re-ran the test.
Much to my surprise, the error persisted and I have been singularly unable to resolve it since despite the following:
- Delete derived data, reboot XCode
- Rename offending entity (error persists, just with the different entity name)
- Delete entity and add back into the Model
- Turn off code-gen and defined the MO subclass manually
- Check Core Data XML - this looks unremarkable and there are no residual properties that could be causing the problem
The one thing I haven't tried yet is to run the code through Xcode 13 (it's a new machine so I was all enthusiastic and went straight for the beta which might have been a mistake!) so will be doing that shortly.
For now it seems that something relating to the Core Data model and relationships between entities is hiding out somewhere I can't find it but it could be a bug in the tooling. The problem of course is that I can't continue to develop the app with a compiler error I can't clear. Any thoughts or guidance would be very welcome...
Xcode Model interface for relevant entities and XML:
Extension on the offending ManagedObject subclass (convenience methods):
extension Prescription {
// MARK: - INTERNAL TYPES
enum PointOfCareContext: String, CustomStringConvertible {
case premedication, perioperative, postoperative, hospitalised
var description: String {
return self.rawValue.capitalized
}
}
// MARK: - PROPERTIES
var drug: Drug {
get { guard let drug = cd_drug else {
fatalError("Persistent store corrupted: Prescription is missing relationship to Drug.")
}
return drug
}
set { cd_drug = newValue }
}
var timePrescribed: Date {
get { guard let timePrescribed = cd_timePrescribed else {
fatalError("Persistent store corrupted: Prescription is missing timePrescribed.")
}
return timePrescribed
}
set { cd_timePrescribed = newValue }
}
/// When the drug should be administered
/// Passing nil to this property indicates the drug should be given asap (so will return current Date)
var timeScheduled: Date? {
get { guard let timeScheduled = cd_timeScheduled else {
return Date.now
}
return timeScheduled
}
set { cd_timeScheduled = newValue }
}
/// When the drug was administered
/// Nil indicates not given yet
var timeAdministered: Date? {
get { return cd_timeAdministered }
set { cd_timeScheduled = newValue }
}
var doseRate: Measurement<UnitDensity> {
get {
if (cd_doseRate != 0) {
fatalError("Persistent store corrupted: Prescription doseRate == 0.")
}
return Measurement(value: cd_doseRate, unit: .milligramsPerKilogram)
}
set {
cd_doseRate = newValue.converted(to: .milligramsPerKilogram).value
}
}
var dose: Measurement<UnitMass> {
get {
return Measurement(value: cd_dose, unit: .milligrams)
}
set {
cd_doseRate = newValue.converted(to: .milligrams).value
}
}
var doseVolume: Measurement<UnitVolume> {
get {
return Measurement(value: cd_doseVolume, unit: .milliliters)
}
set {
cd_doseVolume = newValue.converted(to: .milliliters).value
}
}
var context: PointOfCareContext {
get {
guard let contextValue = cd_context, let context = PointOfCareContext(rawValue: contextValue) else {
fatalError("Persistent store corrupted: Prescription has an invalid administration context [\(cd_context ?? "")]")
}
return context
}
set {
cd_context = newValue.rawValue
}
}
Error encountered when attempting to save managed object context in the unit test: