CoreData question


I made an app based on the landmark app.

One issue I cannot figure out, is how to persist the favorite landmarks.

this code is used:

struct Landmark: Hashable, Codable, Identifiable {
var id: Int
var name: String
fileprivate var imageName: String?
fileprivate var coordinates: Coordinates
var state: String
var park: String
var category: Category
var isFavorite: Bool
var isFeatured: Bool

import Combine

import SwiftUI

final class UserData: ObservableObject {
@Published var showFavoritesOnly = false
@Published var landmarks = landmarkData

and the action button as follows (in Landmarkdetail):

Button(action: {
                }) {
                    if self.userData.landmarks[self.landmarkIndex]
                        .isFavorite {
                        Image(systemName: "star.fill")
                    } else {
                        Image(systemName: "star")

What I already tried, is to Use CoreData within Appdelegate.swift and Scenedelegate.swift files and the following code in Landmarkdetail:

@Environment(\.managedObjectContext) var moc

and under isFavorite.toggle()

                                    if self.moc.hasChanges {


However, it didn't do anything