This is my currentPlayers class
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
}
}
I changed the viewDidLoad code because my currentFav was nil in both conditions and I did not need the post notification there.
override func viewDidLoad() {
super.viewDidLoad()
fetchSave()
if prefArr.count == 0 {
print("There are 0 favourites") //when I add a favourite(s) and the app terminates it goes to this block
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
} else {
print("There are favourites") //when I add a favourite(s) and do not terminate it goes to this block
DispatchQueue.main.asyncAfter(deadline: .now() + 300) {
self.reviewRating.requestReview(isWrittenReview: false)
}
}
}
Whenever I click save it prints 1 more object then I already have regardless of how many favourites there are.
@IBAction func save(_ sender: UIBarButtonItem) {
var 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
print("These are my saved objects: \(saveFav)")
// To save the new entity to the persistent store, call
// save on the context
}
do {
try context.save()
fetchSave() //if I have 53704 saved objects then after I click save I have 53705 it seems to always be over 40000
} catch {
print(error)
}
}
I can easily access my favourite now with HockeyDetailVC.item I made the variable item static. I think it can prove useful. Really appreciate the help.