I have an issue fetching objects from Core Data in my favourites section. I can save it with a button but I can not seem to retrieve the objects when I restart the app. I get it to print over 40000 (I have no idea why) and I see no favourites in my favourites section when I added them before.
override func viewDidLoad() {
super.viewDidLoad()
if currentFav == nil {
//display nil
fetchSave()
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
} else {
NotificationCenter.default.post(name: .passFavNotification,
object: self.currentFav)
DispatchQueue.main.asyncAfter(deadline: .now() + 300) {
self.reviewRating.requestReview(isWrittenReview: false)
}
}
}
//trying to get this function to retrieve my objects in the persistent store
func fetchSave() {
let fetchRequest: NSFetchRequest<CurrentPlayers>
fetchRequest = CurrentPlayers.fetchRequest()
do {
let objects = try context.fetch(fetchRequest)
//I get over 40000 objects
print("These are how many saved favourites I have: \(objects.count)")
tableView.reloadData()
} catch {
print("Fetch failed")
}
}
@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
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()
} catch {
print(error)
}
}