So I have an entity in my core data model called recipe. I need to create another entity containing a recipe and a date that the recipe is assigned to. Can I do this similar to the way I've done it in the image and just save a Recipe object in the initialization of PlannedRecipe object in the Persistence file?
Basically I just need to know how to add a entity in an entity using this core data model and persistence file.
Persistence file:
import CoreData
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "ReciPrep")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
}
func addPlannedRecipe(recipe: Recipe,date: Date, context: NSManagedObjectContext){
let plannedRecipe = PlannedRecipe(context: context)
plannedRecipe.id = UUID()
plannedRecipe.date = Date()
plannedRecipe.recipe = recipe //Giving me an error: "Cannot assign value of type 'Recipe' to type 'Data?'"
save(context: context)
}
func save(context: NSManagedObjectContext){
do {
try context.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
I guess this problem can be solved if I can convert a recipe into binary data or some other savable data in an entity.
Any help would be greatly appreciated.