SwiftUI 1.0: Save custom Datatype in CoreData

264 Views Asked by At

I 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:

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:

struct Catagory: Codable, Identifiable{
   let id: Int
   let localizedString: String
   let engishName: String?
}

When I do it this way I get this error:

Property cannot be declared public because its type uses an internal type

And:

Property cannot be marked @NSManaged because its type cannot be represented in Objective-C

Does anyone have a solution? Thank you in advance.

PS: I use the AppDelegate to create the persistentContainer

EDIT

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:

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.

0

There are 0 best solutions below