AERecord icon indicating copy to clipboard operation
AERecord copied to clipboard

How to do this in AERecord

Open Insfgg99x opened this issue 5 years ago • 0 comments

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
    }

Insfgg99x avatar May 25 '19 05:05 Insfgg99x