Yudishtren Nathan
Yudishtren Nathan

Reputation: 1

Coredata execute Group By sqlite query - iOS SDK

SELECT count(Name),Name from Test group by Name.

How to execute this query using Coredata - iOS SDK

Upvotes: 0

Views: 380

Answers (2)

Adriano Spadoni
Adriano Spadoni

Reputation: 4790

It can be achieved with NSExpression

    // count messages
    let countExpression = NSExpression(format: "count:(message)")
    let countED = NSExpressionDescription()
    countED.expression = countExpression
    countED.name = "messages"
    countED.expressionResultType = .Integer32AttributeType

    // request
    var request = NSFetchRequest()
    request.entity = entityDescription
    request.propertiesToFetch = ["username",messageED]
    request.propertiesToGroupBy = ["username"]

it is important to set the return to dictionary

    request.resultType = .DictionaryResultType

Upvotes: 0

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

you don't.

Core Data is not a database. It is an object hierarchy that happens to persist to disk and one of those persistence choices is a database.

NSPredicate will allow you to retrieve entities from Core Data and you can then use KVC to group them as needed and perform counts.

Upvotes: 1

Related Questions