Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Does Core Data Have A "Created_At" Timestamp?

Tags:

ios

core-data

I just started learning about and using Core Data in my app. Whenever I fetch a list of my saved object, I want to make sure that I ALSO delete the objects that have been in Core Data for longer than a week (will be less when I'm testing, obviously).

How do I get this done? Do these model objects already have an attribute like "createdAt" by default like when other databases give default values like "created_at" and "updated_at"?

If not, I'm thinking of adding a "createdAt" attribute of type Date to my data model, but I'm still kind of confused on how to delete objects that are older than one week after fetching them from Core Data.

Any help or advice would be appreciated, as I am still new to using this powerful tool. Thanks.

like image 282
Rafi Avatar asked Oct 16 '25 14:10

Rafi


1 Answers

Core Data does not automatically create any data for you. That is business logic. If you want a created_at property in your entity, add it and set it in the awakeFromInsert method in your NSManagedObject subclass. You can also set it externally to the object at creation time but putting it in the awakeFromInsert tends to be more consistent.

like image 146
Marcus S. Zarra Avatar answered Oct 18 '25 04:10

Marcus S. Zarra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!