Home > OS >  Swift 5: Is there a default implementation of creationDate and lastEditDate for CoreData Entities?
Swift 5: Is there a default implementation of creationDate and lastEditDate for CoreData Entities?

Time:05-24

I am working with CoreData and am just implementing creationDate and lastEditDate properties for all my entities.


Are there default implementations for this by CoreData that would save this work? I can't find anything on this. Thanks for your help!

CodePudding user response:

You can use awakeFromInsert and willSave. By extending your entity

extension YourEntity{
    public override func awakeFromInsert() {
        if creationDate == nil{
            self.creationDate = Date()
        }
        super.awakeFromInsert()
    }
    
    public override func willSave() {
        self.lastEditDate = Date()
        super.willSave()
    }
}
  • Related