Learn Courses My Dashboard

Fetch Core Data Issue

I have a favourites section in an app and I want the favourites to persist. I think I am saving the data properly into the persistent store but I seem to not be able to retrieve it with my function. Would greatly appreciate the help


import Foundation
import CoreData

enum DecoderConfigurationError: Error {
    case missingManagedObjectContext
}
extension CodingUserInfoKey {
    static let managedObjectContext = CodingUserInfoKey(rawValue: "managedObjectContext")!
}

@objc(CurrentPlayers)
public class CurrentPlayers: NSManagedObject, Decodable {

    
    enum CodingKeys: String, CodingKey {
           case photoUrl = "PhotoUrl"
           case firstName = "FirstName"
           case lastName = "LastName"
           case position = "Position"
           case team = "Team"
           case yahooName = "YahooName"
           case status = "Status"
           case jerseyNumber = "Jersey"
       }
       
       public static var managedObjectContext: NSManagedObjectContext?
       
       required public convenience init(from decoder: Decoder) throws {
           guard let context = decoder.userInfo[.managedObjectContext] as? NSManagedObjectContext else {
               throw DecoderConfigurationError.missingManagedObjectContext
           }
           
          self.init(context: context)
           //...
          let values = try decoder.container(keyedBy: CodingKeys.self)
          photoUrl = try values.decode(String.self, forKey: CodingKeys.photoUrl)
          firstName = try values.decode(String.self, forKey: CodingKeys.firstName)
          lastName =  try values.decode(String.self, forKey: CodingKeys.lastName)
          position = try values.decode(String.self, forKey: CodingKeys.position)
          team = try values.decode(String.self, forKey: CodingKeys.team)
          yahooName = try values.decodeIfPresent(String.self, forKey: CodingKeys.yahooName)
          status = try values.decode(String.self, forKey: CodingKeys.status)
          jerseyNumber = try values.decodeIfPresent(Int64.self, forKey: CodingKeys.jerseyNumber) ?? 0
       }

//this is from FavouritesVC
    
//in my viewdidload does not retrieve the saved object
func fetchSave() { 
        let fetchRequest: NSFetchRequest<CurrentPlayers>
        fetchRequest = CurrentPlayers.fetchRequest()
        do {
            let objects = try context.fetch(fetchRequest)
        } catch {
            print(error)
        }
     }
    
    @IBAction func save(_ sender: UIBarButtonItem) {
        let saveFav = CurrentPlayers(context: context)
        // Assign values to the entity's properties
        for o in prefArr {
        saveFav.yahooName = o.yahooName
        saveFav.team = o.team
        saveFav.position = o.position
        saveFav.photoUrl = o.photoUrl
        // To save the new entity to the persistent store, call
        // save on the context
        }
        do {
            try context.save()
        } catch {
            print(error)
        }
    }

}