Ios 通过谓词CoreData从实体中查找重复记录ID

Ios 通过谓词CoreData从实体中查找重复记录ID,ios,swift,core-data,predicate,Ios,Swift,Core Data,Predicate,如何使用coredata iOS Swift从实体中获取ID为的重复记录。我使用了下面的方法,但没有得到正确的结果。如有任何建议,将不胜感激。提前谢谢 func fetchDuplicateRecords() { let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EmployeeData") let nameExpr = NSExpression(fo

如何使用coredata iOS Swift从实体中获取ID为的重复记录。我使用了下面的方法,但没有得到正确的结果。如有任何建议,将不胜感激。提前谢谢

func fetchDuplicateRecords() {
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EmployeeData")
    
    let nameExpr = NSExpression(forKeyPath: "name")
    
    let countExpr = NSExpressionDescription()
    let countVariableExpr = NSExpression(forVariable: "Id")
    
    countExpr.name = "count"
    countExpr.expression = NSExpression(forFunction: "count:", arguments: [ nameExpr, nameExpr1, nameExpr2 ])
    countExpr.expressionResultType = .integer64AttributeType
    do {
        fetchRequest.resultType = .dictionaryResultType
        let idDescriptor: NSSortDescriptor = NSSortDescriptor(key: "Id", ascending: true)
        fetchRequest.sortDescriptors = [idDescriptor]
        
        fetchRequest.propertiesToGroupBy = ["name"]
        fetchRequest.propertiesToFetch = ["name", countExpr]
        fetchRequest.havingPredicate = NSPredicate(format: "%ld > 1", countVariableExpr)
        
        //let results = try managedContext.execute(fetchRequest) as AnyObject
        let searchResults = try managedContext.fetch(fetchRequest)
        let resultsDict = searchResults as! [[String: Int32]]
        print("\(resultsDict)")
    } catch {
    }
}
func fetchDuplicateRecords(){
let fetchRequest=NSFetchRequest(entityName:“EmployeeData”)
让nameExpr=NSExpression(forKeyPath:“name”)
让countExpr=NSExpressionDescription()
让countVariableExpr=NSExpression(for变量:“Id”)
countExpr.name=“count”
countExpr.expression=NSExpression(函数:“count:”,参数:[namexpr,namexpr1,namexpr2])
countExpr.expressionResultType=.integer64AttributeType
做{
fetchRequest.resultType=.dictionaryResultType
让idDescriptor:NSSortDescriptor=NSSortDescriptor(键:“Id”,升序:true)
fetchRequest.sortDescriptors=[idDescriptor]
fetchRequest.propertiesToGroupBy=[“名称”]
fetchRequest.propertiesToFetch=[“名称”,countExpr]
fetchRequest.havingPredicate=NSPredicate(格式:“%ld>1”,countVariableExpr)
//让results=try managedContext.execute(fetchRequest)作为AnyObject
让searchResults=尝试managedContext.fetch(fetchRequest)
让resultsDict=searchResults为![[String:Int32]]
打印(“\(resultsDict)”)
}抓住{
}
}
应为计数:

然后,谓词将在count>1的位置进行比较

NSPredicate(format: "%@ > 1", countVariableExpr)
编辑:
您当前正在按名称查找重复记录,如果您想按id查找,则可能是这样的:

let nameExpr = NSExpression(forKeyPath: "name")
需要更改为:

let idExpression = NSExpression(forKeyPath: "Id")
然后计数表达式将更改为计数记录ID

NSExpression(forFunction: "count:", arguments: [idExpression])
编辑2:

fetchRequest.propertiesToGroupBy = ["name"]
fetchRequest.propertiesToFetch = ["name", countExpr]
还应更改为获取Id而不是名称


嗨,谢谢你的回复。我也尝试过这种方法,但没有在搜索结果中获得id。我编辑了我的答案,检查它现在是否对您有效。
NSExpression(forFunction: "count:", arguments: [idExpression])
fetchRequest.propertiesToGroupBy = ["name"]
fetchRequest.propertiesToFetch = ["name", countExpr]
fetchRequest.propertiesToGroupBy = ["Id"]
fetchRequest.propertiesToFetch = ["Id", countExpr]