Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if core data is empty

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”

like image 873
Ahmed R. Avatar asked Apr 21 '15 10:04

Ahmed R.


People also ask

How do I delete all Core Data?

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.

Where does Core Data store 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.

What is your Core Data?

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.


1 Answers

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
    }
}
like image 122
Maor Avatar answered Nov 09 '22 14:11

Maor