diff --git a/PortfolioJournal/Models/CoreData/Account+CoreDataClass.swift b/PortfolioJournal/Models/CoreData/Account+CoreDataClass.swift index 4d30d43..200c95f 100644 --- a/PortfolioJournal/Models/CoreData/Account+CoreDataClass.swift +++ b/PortfolioJournal/Models/CoreData/Account+CoreDataClass.swift @@ -15,7 +15,13 @@ public class Account: NSManagedObject, Identifiable { get { safeValue(forKey: "name", fallback: "") } set { setManagedValue(newValue, forKey: "name") } } - @NSManaged public var createdAt: Date + // createdAt is optional in the model; a non-optional @NSManaged Date crashes the + // CloudKit merge when an imported record materialises with a transient nil. + // Same nil-tolerant accessor pattern already applied to Snapshot. + @objc public var createdAt: Date { + get { safeValue(forKey: "createdAt", fallback: .distantPast) } + set { setManagedValue(newValue, forKey: "createdAt") } + } @NSManaged public var currency: String? @NSManaged public var inputMode: String @NSManaged public var notificationFrequency: String diff --git a/PortfolioJournal/Models/CoreData/Goal+CoreDataClass.swift b/PortfolioJournal/Models/CoreData/Goal+CoreDataClass.swift index e8adad3..9a0dcc7 100644 --- a/PortfolioJournal/Models/CoreData/Goal+CoreDataClass.swift +++ b/PortfolioJournal/Models/CoreData/Goal+CoreDataClass.swift @@ -18,7 +18,13 @@ public class Goal: NSManagedObject, Identifiable { @NSManaged public var targetAmount: NSDecimalNumber? @NSManaged public var targetDate: Date? @NSManaged public var isActive: Bool - @NSManaged public var createdAt: Date + // createdAt is optional in the model; a non-optional @NSManaged Date crashes the + // CloudKit merge when an imported record materialises with a transient nil. + // Same nil-tolerant accessor pattern already applied to Snapshot. + @objc public var createdAt: Date { + get { safeValue(forKey: "createdAt", fallback: .distantPast) } + set { setManagedValue(newValue, forKey: "createdAt") } + } @NSManaged public var account: Account? public override func awakeFromInsert() { diff --git a/PortfolioJournal/Models/CoreData/InvestmentSource+CoreDataClass.swift b/PortfolioJournal/Models/CoreData/InvestmentSource+CoreDataClass.swift index c36f2e4..cfab644 100644 --- a/PortfolioJournal/Models/CoreData/InvestmentSource+CoreDataClass.swift +++ b/PortfolioJournal/Models/CoreData/InvestmentSource+CoreDataClass.swift @@ -19,7 +19,13 @@ public class InvestmentSource: NSManagedObject, Identifiable { @NSManaged public var customFrequencyMonths: Int16 @NSManaged public var isActive: Bool @NSManaged public var monthlyContribution: NSDecimalNumber? - @NSManaged public var createdAt: Date + // createdAt is optional in the model; a non-optional @NSManaged Date crashes the + // CloudKit merge when an imported record materialises with a transient nil. + // Same nil-tolerant accessor pattern already applied to Snapshot. + @objc public var createdAt: Date { + get { safeValue(forKey: "createdAt", fallback: .distantPast) } + set { setManagedValue(newValue, forKey: "createdAt") } + } @NSManaged public var category: Category? @NSManaged public var account: Account? @NSManaged public var snapshots: NSSet?