Swift CoreStore订购列表监视器问题

时间:2019-08-15 19:59:25

标签: ios swift nsfetchedresultscontroller nsfetchrequest corestore

我有NSManagedObject数据模型ExerciseEntity,也有数据模式MuscleEntity。我可以通过exercise.muscle

访问肌肉对象

我添加了CoreStore监视器来观察数据模型的变化。

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

我希望得到的结果是我所有的部分都是我的肌肉名称,并且每个部分都按照以下顺序进行了锻炼:

Abs section
1 exercise abs
2 exercise abs
Arms section
01 exercise arms
02 exercise arms

期望结果监视器的返回为我交换了结果,因为01和02字符串<1和2练习abs字符串按升序排列:

Abs section
01 exercise arms
02 exercise arms
Arms section
1 exercise abs
2 exercise abs

我只希望此升序排序规则在部分内部应用,而不是在整个列表中应用。

我已经打印了监视器:

    (lldb) po monitor
    (CoreStore.ListMonitor<Stacked.ExerciseEntity>) (
    .isPendingRefetch = false;
    .numberOfObjects = 140;
    .sections = 16 item(s) [
        0 = "Abs" (
            .numberOfObjects = 13;
            .indexTitle = "Optional("Abs") years old";
        );
        1 = "Arms" (
            .numberOfObjects = 1;
            .indexTitle = "Optional("Arms") years old";
        );

您可以看到第0部分是Abs,第1部分是arm,好吧,让我们看一下对象:

        print("Section group name: ", monitor.sectionInfo(at: 0).name)
        for e in monitor.objects(in: 0) {
            print("Exercis name", e.name!, " Should connected to section named:", e.muscle!.name!)
        }

        print("Section group name: ", monitor.sectionInfo(at: 1).name)
        for e in monitor.objects(in: 1) {
            print("Exercis name", e.name!, " Should connected to section named:", e.muscle!.name!)
        }

如果您不想阅读所有输出,则可以注意到第一个练习已连接到Arms,但是它以某种方式出现在Abs部分中。

Section group name:  Abs
Exercis name 1111  Should connected to section named: Arms

完整输出:

Section group name:  Abs
Exercis name 1111  Should connected to section named: Arms
Exercis name Ab Crunch Machine  Should connected to section named: Abs
Exercis name Abdominal Rollout  Should connected to section named: Abs
Exercis name Air Bicycles  Should connected to section named: Abs
Exercis name Alternating Dumbbell Curl  Should connected to section named: Arms (Biceps)
Exercis name Arnold Dumbbell Press  Should connected to section named: Shoulders (Anterior Deltoids)
Exercis name Barbell Bench Press  Should connected to section named: Chest
Exercis name Barbell Curl  Should connected to section named: Arms (Biceps)
Exercis name Barbell Deadlift  Should connected to section named: Back
Exercis name Barbell Front Squat  Should connected to section named: Legs (Quadriceps)
Exercis name Barbell Hip Thrust  Should connected to section named: Glutes
Exercis name Barbell Holds  Should connected to section named: Arms (Forearms)
Exercis name Barbell Lunge (In-Place)  Should connected to section named: Legs (Quadriceps)
Section group name:  Arms
Exercis name Barbell Lunge (Reverse)  Should connected to section named: Legs (Quadriceps)

您认为所有练习都按以下规则排序:.orderBy(.ascending(\.name))

我想按节排序。

所以Abs应该只包含Abs运动,而arms部分应该仅包含Arms运动。

1 个答案:

答案 0 :(得分:0)

您要告诉ListMonitor到.orderBy(.ascending(\.name)),所以这正是按字母顺序执行的操作:

01 exercise arms
02 exercise arms
1 exercise abs
2 exercise abs

您将需要另一个属性进行排序,以尊重您的部分分组。