ios - CoreStore 分段列表监视器如何在运行时指定 .where 子句

标签 ios swift core-data nspredicate corestore

我的初始化方法中有这段代码:

self.monitor = CoreStore.monitorSectionedList(
        From<ListEntityType>()
            .sectionBy(#keyPath(ListEntityType.muscle.name)) { (sectionName) -> String? in
                return "\(String(describing: sectionName)) years old"
            }
            .orderBy(.ascending(#keyPath(ListEntityType.muscle.name)), .ascending(\.name))
    )

我想以某种方式在运行时向此监视器添加 .where 条件。

ListEntityType 是名为 Exercise 的实体的类型别名。所以每个 Exercise 都包含一对一关系 Muscle (exercise.muscle)。

每个实体都有唯一的标识符属性。

我有一组肌肉标识符,我想将其用作过滤器以显示分段列表中的对象。

如何循环遍历此标识符以将 then to .where 子句添加到 CoreStore.monitorSectionedList

我可能期待这样的事情但不是 100% 肯定只是假设:

让 predicate = NSPredicate(格式: "%K = $ARGUMENT")

    var predicates = [NSPredicate]()

    if let muscles : [MuscleGroupEntity] = workout?.muscles.toArray() {
        for muscle in muscles
        {
            let myNewPredicate = predicate.withSubstitutionVariables(["ARGUMENT" : muscle.id])
            predicates.append(myNewPredicate)
        }
    }

    self.monitor = CoreStore.monitorSectionedList(
        From<ListEntityType>()
            .sectionBy(#keyPath(ListEntityType.muscle.name)) { (sectionName) -> String? in
                return "\(String(describing: sectionName)) years old"
            }
            .where(format:"%K = %@", argumentArray: predicates)
            .orderBy(.ascending(#keyPath(ListEntityType.muscle.name)), .ascending(\.name))
    )

此代码因错误而崩溃:

-[NSComparisonPredicate rangeOfString:]: unrecognized selector sent to

我想这是我发现的谓词 here ,但不确定如何在我的情况下正确使用它

已编辑 MartinM 评论:

我有一个带有属性 Muscle 的 ExerciseEntity。

如果我想只使用一个肌肉 id 进行所有练习,那非常简单:

.where(format:"%K = %@", #keyPath(ExerciseEntity.muscle.id), "id_12345")

但我想指定更多的肌肉并在运行时定义它们。不知何故,我需要了解格式是什么、参数是什么以及如何传递标识符数组而不是一个 "id_12345"

最佳答案

如果您想获取所有 ExerciseEntity,其中 muscle.id 包含在 muscleId 列表中,您只需使用:

let muscleIds = workout?.muscles.compactMap({ $0.id }) ?? []
From....
   .where(Where<ExerciseEntity>(#keyPath(ExerciseEntity.muscle.id), isMemberOf: muscleIds))

这相当于一个谓词,格式为:%K IN %@, ExerciseEntity.muscle.id, muscleIds

这也适用于取反 -> NOT(%K IN %@),就像您可以在 CoreStore 中使用 !Where 一样。

复合谓词需要指定它们的链接方式。 CoreStore 一次支持 2 个谓词的简写,使用逻辑运算符,例如:

Where<ExerciseEntity>(yourWhereClause) && Where<ExerciseEntity(otherWhereClause)

这相当于像这样使用 NSCompoundPredicate:

NSCompoundPredicate(type: .and, subpredicates: [left.predicate, right.predicate])

如果您需要更复杂的连词,您也可以将该复合谓词作为参数直接传递给 where 子句。然后,您还可以将该复合谓词存储在某处,并附加另一个符合您需要的复合谓词。

关于ios - CoreStore 分段列表监视器如何在运行时指定 .where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731824/

相关文章:

ios - 立方体贴图未显示纹理 (OpenGL ES)

iphone - 带 URL 的 writeToFile - URL 在下一个 session 中不存在

iOS ARKit 指向 anchor 的指针

swift - ios12系统图像按钮的色调颜色不起作用

ios - CoreFoundation 陷阱错误 "Thread1:EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)"

iphone - 带边框的 UIImage。无效上下文 0x0

php - iOS 推送通知消息 - 单击 VIEW 按钮后的操作

ios - 检查 Swift 中是否存在全局函数

ios - 核心数据 : performance issue. 仪器的输出是神秘的

ios - 如何检查关系是否已经建立 - Core Data