【问题标题】:Swift CoreStore ordering list monitor issueSwift CoreStore 订单列表监控问题
【发布时间】:2019-08-15 19:59:25
【问题描述】:

我有 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 字符串是

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 部分是手臂,好吧,让我们看看对象:

        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 运动,而手臂部分应该只包含手臂运动。

【问题讨论】:

    标签: ios swift nsfetchedresultscontroller nsfetchrequest corestore


    【解决方案1】:

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

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

    您将需要另一个属性来排序,这将尊重您的部分分组。

    【讨论】:

    • 感谢约翰的回答,但这是否可以仅在部分中订购元素。假设我有一个拥有财产国家的城市实体。然后国家是我的组(一个部分),城市是仅为这个适当国家订购的城市列表。我不关心下一节将如何排序,但假设下一节将从 A-Z 排序,就像第一个一样。
    • 或者您可以简单地理解我使用此image 提到的内容,这里是由 Staes 分隔的城市,但每个部分的名称都是升序。所以假设我有相同的城市和财产状态。所以我想使用加利福尼亚或纽约等州名对它们进行分组,并按升序排列每个部分中的城市
    • @MatrosovAlexander 您可以提供多个排序键:.orderBy(.ascending(\.state), .ascending(\.name))
    • 感谢为我工作!!!据我了解,这些.ascending(\.state), .ascending(\.name) 的顺序很重要吗?还有我如何订购它忽略大小写,例如忽略大写,以按字母顺序排列元素,目前所有 ABC 在列表顶部,abc 在底部,但在我的情况下排序时最好忽略大小写。
    • 如果你想做一个不区分大小写的比较,你需要自己创建你的排序描述符并传入选择器来这样做。据我所知,corestore 中没有可用的速记包装器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    相关资源
    最近更新 更多