AERecord
AERecord copied to clipboard
How to do this in AERecord
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
}