Hi, in my current project I want to save a custom data type in core data but I don't know to do it.
The Core Data Entity:
My own data type:
When I do it this way I get this error:
When Catagory is a own core data entity the app is starting but when I add an object I got the following error like 10 times in the console:
PS: I use the AppDelegate to create the persistentContainer
The Core Data Entity:
Code Block extension WorkoutBuy { @nonobjc public class func fetchRequest() -> NSFetchRequest<WorkoutBuy> { return NSFetchRequest<WorkoutBuy>(entityName: "WorkoutBuy") } @NSManaged public var beschreibung: String? @NSManaged public var dauerInterval: Int64 @NSManaged public var dauerPause: Int64 @NSManaged public var dauerPauseDurchgang: Int64 @NSManaged public var durchgaenge: Int64 @NSManaged public var id: UUID? @NSManaged public var intensitaet: String? @NSManaged public var name: String? @NSManaged public var price: Double @NSManaged public var catagory: Catagory? !!!//here is my own data type !!! } extension WorkoutBuy : Identifiable { }
My own data type:
Code Block struct Catagory: Codable, Identifiable{ let id: Int let localizedString: String let engishName: String? }
When I do it this way I get this error:
And:Property cannot be declared public because its type uses an internal type
One thing I tried:Property cannot be marked @NSManaged because its type cannot be represented in Objective-C
When Catagory is a own core data entity the app is starting but when I add an object I got the following error like 10 times in the console:
Does anyone have a solution? Thank you in advance.2020-12-04 21:58:31.586858+0100 IntervallTraining[4869:1923447] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null) CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null) Ein Core Data-Fehler ist aufgetreten.
PS: I use the AppDelegate to create the persistentContainer
The error messages are showing the fact.
Core Data allows us very limited set of data types as property, where Swift struct is not included.
You can change the type of catagory to Data and store the encoded Data of Catagory into it.
And you need to decode it to Catagory when using it.
(You can write a simple extension to work with it.)
If the custom data type was an NSObject descendant, you could use some Transformable feature of Core Data.
But this is not an option when you want to use Swift struct.
You cannot use Swift struct as a property of Core Data.
Core Data allows us very limited set of data types as property, where Swift struct is not included.
You can change the type of catagory to Data and store the encoded Data of Catagory into it.
And you need to decode it to Catagory when using it.
(You can write a simple extension to work with it.)
If the custom data type was an NSObject descendant, you could use some Transformable feature of Core Data.
But this is not an option when you want to use Swift struct.