How do I check if core data is empty using Swift. I tried this method:
var people = [NSManagedObject]()
if people == nil {
}
but this results in this error:
“binary operator '==' cannot be applied to operands of type [NSManagedObject] and nil”
One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.
The persistent store should be located in the AppData > Library > Application Support directory. In this example you should see a SQLite database with extension . sqlite. It is possible that you don't see the persistent store in the Application Support directory.
Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.
Swift 3 solution:
var isEmpty: Bool {
do {
let request = NSFetchRequest(entityName: YOUR_ENTITY)
let count = try context.count(for: request)
return count == 0
} catch {
return true
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With