Swift CoreStore ordering list monitor issue

182 Views Asked by At

I have NSManagedObject data model ExerciseEntity also I have data mode MuscleEntity. I can access muscle object via exercise.muscle

I've adde CoreStore monitor to observe changes to data model.

    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))
        )

I expect result where I have all sections which is my muscle names and each section has ordered exercises in it like:

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

Insead of expected result monitor returns for me swaped result as 01 and 02 string is < 1 and 2 exercise abs string in ascending order:

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

I just want this ascending ordering rule to be applied internally in section, not for entire list.

I have printed monitor:

    (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";
        );

As you can see section 0 is Abs and section 1 is arms, ok let's take a look of objects:

        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!)
        }

If you don't want to read all output you can notice that the first exercise is connected to Arms, but it somehow appeared in the Abs section.

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

Full Output:

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)

Thought as you see all exercises are ordered right by this rules: .orderBy(.ascending(\.name))

By I suppose ordering through section.

So Abs should contains only Abs exercise and arms section should contains only arms exercise.

1

There are 1 best solutions below

6
John Estropia On

You are telling the ListMonitor to .orderBy(.ascending(\.name)), so that's exactly what it does alphabetically:

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

You will need another property to sort on that will respect your section grouping.