iOS 13.x上的CoreData NSExpression“ max:”函数行为

假设我们在Core Data模型中有一个非常简单的实体,名称为“ Entity”,它具有一个名为“ Integer 32”类型的属性“ index”。这个实体是空的。 然后我们运行以下代码:

        let context: NSmanagedobjectcontext = getcoreDataContext()
        let maxIndexKeypath = "maxIndex"

        let request: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Entity")
        request.resultType = NSFetchRequestResultType.dictionaryResultType

        let expressionDescription = NSExpressionDescription()
        expressionDescription.name = maxIndexKeypath
        let keypathExpression = NSExpression(forKeyPath: "index")
        expressionDescription.expression = NSExpression(forFunction: "max:",arguments: [keypathExpression])
        expressionDescription.expressionResultType = .integer32AttributeType

        request.propertiesToFetch = [expressionDescription]
        var maxIndex: Int?
        do {
            let result = try context.fetch(request) as? [[String: Int32]]
            print(result?.isEmpty)
            print(result)
            if let result = try context.fetch(request) as? [[String: Int32]],let maxIndexDict = result.first {
                maxIndex = maxIndexDict[maxIndexKeypath]?.int
            }
        } catch {
            fatalError()
        }
        print(maxIndex)

iOS 12.2 上,打印结果为:

Optional(false)
Optional([["maxIndex": 0]])
Optional(0)

iOS> = 13(当前为iOS 13.2)上,打印结果为:

Optional(false)
Optional([[:]])
nil

我的问题:这是iOS 13(及更高版本)中在实体为空的情况下计算属性最大值的错误吗?
还有第二个问题:在两种情况下(低于iOS 13和高于iOS 13)计算空实体的最大值的最佳解决方法(如果第一个问题的答案为真)是什么?

lipanhaoran 回答:iOS 13.x上的CoreData NSExpression“ max:”函数行为

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3152013.html

大家都在问