Sorting SwiftData module records based on recent update

Hi,

I know how to sort SwiftData records based on one of its properties such as name, but how to sort based on recent updated records ? like in Apple Notes App ?

Kind Regards

Answered by DTS Engineer in 798468022

SwiftData doesn't provide the modified date for your objects, and so you need to add and maintain your own attribute to record the modified date of your objects, and use the attribute to sort.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData doesn't provide the modified date for your objects, and so you need to add and maintain your own attribute to record the modified date of your objects, and use the attribute to sort.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks allot, so is there any API or observation method to get notified when some record is changed or that should be done manually as well ?

Kind Regards

SwiftData provides didSave for you to get notified when the model context is saved. The doc says that the notification’s userInfo dictionary contains the persistent identifiers of any inserted, updated, or deleted models.

However, didSave doesn't work yet as of today, and so you might consider using the Core Data notification (.NSManagedObjectContextDidSave) instead.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Sorting SwiftData module records based on recent update
 
 
Q