Cocoa Core Data efficient way to count entities

Objective CCore Data

Objective C Problem Overview


I read much about Core Data.. but what is an efficient way to make a count over an Entity-Type (like SQL can do with SELECT count(1) ...). Now I just solved this task with selecting all with NSFetchedResultsController and getting the count of the NSArray! I am sure this is not the best way...

Objective C Solutions


Solution 1 - Objective C

I don't know whether using NSFetchedResultsController is the most efficient way to accomplish your goal (but it may be). The explicit code to get the count of entity instances is below:

// assuming NSManagedObjectContext *moc

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:moc]];

[request setIncludesSubentities:NO]; //Omit subentities. Default is YES (i.e. include subentities)

NSError *err;
NSUInteger count = [moc countForFetchRequest:request error:&err];
if(count == NSNotFound) {
  //Handle error
}

[request release];

Solution 2 - Objective C

To be clear, you aren't counting entities, but instances of a particular entity. (To literally count the entities, ask the managed object model for the count of its entities.)

To count all the instances of a given entity without fetching all the data, the use -countForFetchRequest:.

For example:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: [NSEntityDescription entityForName: entityName inManagedObjectContext: context]];

NSError *error = nil;
NSUInteger count = [context countForFetchRequest: request error: &error];

[request release];

return count;

Solution 3 - Objective C

Swift

It is fairly easy to get a count of the total number of instances of an entity in Core Data:

let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "MyEntity")
let count = context.countForFetchRequest(fetchRequest, error: nil)

I tested this in the simulator with a 400,000+ object count and the result was fairly fast (though not instantaneous).

Solution 4 - Objective C

I'll just add that to make it even more efficient... and because its just a count, you don't really need any property value and certainly like one of the code examples above you don't need sub-entities either.

So, the code should be like this:

int entityCount = 0;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourEntity" inManagedObjectContext:_managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setIncludesPropertyValues:NO];
[fetchRequest setIncludesSubentities:NO];
NSError *error = nil;
NSUInteger count = [_managedObjectContext countForFetchRequest: fetchRequest error: &error];
if(error == nil){
    entityCount = count;
}

Hope it helps.

Solution 5 - Objective C

I believe the easiest and the most efficient way to count objects is to set NSFetchRequest result type to NSCountResultType and execute it with NSManagedObjectContext countForFetchRequest:error: method.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:entityName];
fetchRequest.resultType = NSCountResultType;
NSError *fetchError = nil;
NSUInteger itemsCount = [managedObjectContext countForFetchRequest:fetchRequest error:&fetchError];
if (itemsCount == NSNotFound) {
    NSLog(@"Fetch error: %@", fetchError);
}

// use itemsCount

Solution 6 - Objective C

I wrote a simple utility method for Swift 3 to fetch the count of the objects.

static func fetchCountFor(entityName: String, predicate: NSPredicate, onMoc moc: NSManagedObjectContext) -> Int {
    
    var count: Int = 0
            
    moc.performAndWait {
        
        let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: entityName)
        fetchRequest.predicate = predicate
        fetchRequest.resultType = NSFetchRequestResultType.countResultType
        
        do {
            count = try moc.count(for: fetchRequest)
        } catch {
            //Assert or handle exception gracefully
        }
        
    }
    
    return count
}

Solution 7 - Objective C

In Swift 3

  static func getProductCount() -> Int {
    let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Product")
    let count = try! moc.count(for: fetchRequest)
    return count
}

Solution 8 - Objective C

It's really just this:

let kBoat = try? yourContainer.viewContext.count(for: NSFetchRequest(entityName: "Boat"))

"Boat" is just the name of the entity from your data model screen:

> enter image description here

What is the global yourContainer?

To use core data, at some point in your app, one time only, you simply go

var yourContainer = NSPersistentContainer(name: "stuff")

where "stuff" is simply the name of the data model file.

> enter image description here

You'd simply have a singleton for this,

import CoreData
public let core = Core.shared
public final class Core {
    static let shared = Core()
    var container: NSPersistentContainer!
    private init() {
        container = NSPersistentContainer(name: "stuff")
        container.loadPersistentStores { storeDescription, error in
            if let error = error { print("Error loading... \(error)") }
        }
    }
    
    func saveContext() {
        if container.viewContext.hasChanges {
            do { try container.viewContext.save()
            } catch { print("Error saving... \(error)") }
        }
    }
}

So from anywhere in the app

core.container

is your container,

So in practice to get the count of any entity, it's just

let k = try? core.container.viewContext.count(for: NSFetchRequest(entityName: "Boat"))

Solution 9 - Objective C

If you want to find count for specific predicated fetch, i believe this is the best way:

NSError *err;
NSUInteger count = [context countForFetchRequest:fetch error:&err];

if(count > 0) {
NSLog(@"EXIST"); 
} else {
NSLog(@"NOT exist");
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionerazorxView Question on Stackoverflow
Solution 1 - Objective CBarry WarkView Answer on Stackoverflow
Solution 2 - Objective CJim CorreiaView Answer on Stackoverflow
Solution 3 - Objective CSuragchView Answer on Stackoverflow
Solution 4 - Objective COscar SalgueroView Answer on Stackoverflow
Solution 5 - Objective CYuriy PavlyshakView Answer on Stackoverflow
Solution 6 - Objective CjaroraView Answer on Stackoverflow
Solution 7 - Objective CPhilippe H. RegenassView Answer on Stackoverflow
Solution 8 - Objective CFattieView Answer on Stackoverflow
Solution 9 - Objective CUmit KayaView Answer on Stackoverflow