I have a UNNotificationServiceExtension
that downloads videos and images to the Documents
directory for use by classes that adopt UNNotificationContentExtension
. I want to delete the media files that are no longer being used by any notifications. I am not sure how to go about doing this.
- I tried to delete the files in my AppDelegate, but I believe the
UNNotificationServiceExtension
has its ownDocuments
directory per the "Sharing Data With Your Containing App" section of this document: https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html, so I cannot access these files from my main app. They are in a different container. - I don't want to create an App Group to share the data between the app and the extension just so that I can delete the unused files.
- I don't want to delete the unused files in the
UNNotificationServiceExtension
, because the extension has a limited amount of time in which to complete its work, and if I try to download files and delete other files, it may time out.
I think the best option is to check to see which files are needed by any delivered notifications and to delete the unneeded files in the Notification Service Extension's Documents
directory. My concern with this is that the UNNotificationServiceExtension
is only given a short period of time during which it must complete all of its work, after which it will time out.
So, my question is, "Is this the right way to clean up unused files from a Notification Service Extension, or is there a better way?"