Basically I have set up some up an entity to store some data. However, there will be no entities in CoreData until the user starts to perform some actions. I'm trying to perform some sort of code that just checks to see if any entities exist for a given entity parameter. So if my entity has parameters of name: String and number: Int. I want to know if any entities exist for name: "Example".
I have tried a bunch of different solutions but my apps keeps crashing saying that fetch requests cannot be performed when there are no entities. This is my current code I was trying to use to see if there was anything there.
func isThereFlashCardAccData(cardName: String) -> Bool {
let fR = NSFetchRequest<FlashCardAccuracy>()
fR.predicate = NSPredicate(format: "cardName == %@", cardName)
do {
let count = try viewContext.count(for: fR)
if count == 0 {
return false
}else {
return true
}
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
return false
}
}
Try using the fetchRequest() helper function on the entity. Double check your entity name and make sure it matches.