Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do this in AERecord #51

Open
Insfgg99x opened this issue May 25, 2019 · 0 comments
Open

How to do this in AERecord #51

Insfgg99x opened this issue May 25, 2019 · 0 comments

Comments

@Insfgg99x
Copy link

Insfgg99x commented May 25, 2019

Sum a property:

    func totalUnreadCount(whereParentChatId: String?) -> Int {
        let req = NSFetchRequest<NSDictionary>.init(entityName: Conversation.entityName())
        if let parentChatId = whereParentChatId {
            req.predicate = NSPredicate.init(format: "parentChatId=%@", parentChatId)
        } else {
            req.predicate = NSPredicate.init(format: "parentChatId = nil")
        }
        let expressDescription = NSExpressionDescription.init()
        let express = NSExpression.init(format: "@sum.unreadCount")
        expressDescription.expression = express
        expressDescription.name = "total"
        expressDescription.expressionResultType = .integer32AttributeType
        req.propertiesToFetch = [expressDescription]
        req.resultType = .dictionaryResultType
        var count = 0
        dbContext().performAndWait {
            let result = try? dbContext().fetch(req)
            if let total = result?.first?["total"] as? Int {
                count = total
            }
        }
        return count
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant