有没有一种方法可以按添加对象的时间顺序对核心数据进行排序?

编程入门 行业动态 更新时间:2024-10-23 11:24:15
本文介绍了有没有一种方法可以按添加对象的时间顺序对核心数据进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我只是将模拟的data添加到coreData中,如下所示:

If I just add mocked data to coreData like this:

func mockData() {´ let entity = NSEntityDescription.entity(forEntityName: "LocalDoorCoreDataObject", in: context) for i in 1...3 { var myEntity = MyCoreDataObject(entity: entity!, insertInto: context) myEntity.dName = "MOCKED NAME \(i)" } }

但是当我fetch data时,它不是按1、2、3顺序排列的,而是随机列出的.有没有一种方法可以完全按照store到coreData的顺序来store和fetch data?这很重要,因为我希望能够通过将cells的顺序拖到另一个位置,然后将更改存储到coreDatabase来重新排列.

But when I fetch the data it's not ordered 1, 2, 3, but is rather listed randomly. Is there a way to store and fetch the data exactly in the order you store it to the coreData? This is important since I want to be able to rearrange the order of the cells by dragging them to a different place, then storing the changes to coreDatabase.

推荐答案

  • 在NSManagedObject子类中添加一个属性(当然也在模型中)

  • In the NSManagedObject subclass add an attribute (of course also in the model) @NSManaged var itemAdded : Date

  • 覆盖awakeFromInsert

    override func awakeFromInsert() { super.awakeFromInsert() itemAdded = Date() }

  • 现在您可以按itemAdded

    更多推荐

    有没有一种方法可以按添加对象的时间顺序对核心数据进行排序?

    本文发布于:2023-10-26 23:27:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1531654.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:顺序   对象   核心   时间   方法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!