All Products
Document Center

Data cleanup

Last Updated: May 25, 2021

Automatically cleaned cache directory

Create an automatically cleaned cache directory. Specify the cleaning logic by using APPurgeableType and specify the size of the cache directory by using size. Each time the app starts, it checks the directory status in the background process and deletes files as needed. If you set an upper limit for the directory’s capacity, when the directory reaches the upper limit, the app deletes the earliest files to restore the capacity of the cache directory to 1/2 of the upper limit.

  1. #import <Foundation/Foundation.h>
  2. typedef NS_ENUM(NSUInteger, APPurgeableType)
  3. {
  4. APPurgeableTypeManual = 0, // Clear the data when user manually clear the cache.
  5. APPurgeableTypeThreeDays = 3, // Automatically delete the data that was generated three days ago.
  6. APPurgeableTypeOneWeek = 7, // Automatically delete the data that was generated one week ago.
  7. APPurgeableTypeTwoWeeks = 14, // Automatically delete the data that was generated two weeks ago.
  8. APPurgeableTypeOneMonth = 30, // Automatically delete the data that was generated one month ago.
  9. };
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif // __cplusplus
  13. /**
  14. * Return a cleanable storage path based on the user's input, and automatically determine whether the directory exists. If not, create one.
  15. *
  16. * @param userPath: The user-specified path. For example, the path is previously concatenated by using "Documents/SomePath" and is now obtained conveniently by using APPurgeableStoragePath(@"Documents/SomePath").
  17. * @param type: Specify a cleaning type, which can be manual, every week, or every three days.
  18. * @param size: Specify a maximum data size in MB. Earlier data is cleared when the maximum size is reached. 0 indicates that there is no upper limit.
  19. *
  20. * @return Target path
  21. */
  22. NSString* APPurgeablePath(NSString* path);
  23. NSString* APPurgeablePathType(NSString* path, APPurgeableType type);
  24. NSString* APPurgeablePathTypeSize(NSString* path, APPurgeableType type, NSUInteger size /* MB */);
  25. /**
  26. * Clear and reset all registered directories.
  27. */
  28. void ResetAllPurgeablePaths();
  29. #ifdef __cplusplus
  30. }
  31. #endif // __cplusplus

Cache cleaning API

Data Center provides a cache cleaning implementation class, which reads cleaning tasks from PurgeableCache.plist. This file must be delivered in the Main Bundle of the app. The cleaner is executed asynchronously. The callback function is always called in the main thread and can be used to display and handle UIs.

  1. #import <Foundation/Foundation.h>
  2. typedef NS_ENUM(NSUInteger, APCacheCleanPhase)
  3. {
  4. APCacheCleanPhasePreCalculating = 0, // Scan the sandbox size before cleaning.
  5. APCacheCleanPhaseCleaning, // Cleaning
  6. APCacheCleanPhasePostCalculating, // Scan the sandbox size after cleaning is completed.
  7. APCacheCleanPhaseDone, // Cleaned
  8. };
  9. @interface APUserCacheCleaner : NSObject
  10. /**
  11. * Perform cleaning asynchronously. A callback method must be transferred.
  12. * progress indicates the real progress when phase returns APCacheCleanPhasePreCalculating, APCacheCleanPhaseCleaning, or APCacheCleanPhasePostCalculating. The maximum value is 1.0.
  13. * progress returns the amount of cleaned data in MB when phase is APCacheCleanPhaseDone.
  14. *
  15. * @param callback: Callback method.
  16. */
  17. + (void)execute:(void(^)(APCacheCleanPhase phase, float progress))callback;
  18. @end

Two types of cleaning tasks can be defined in PurgeableCache.plist.image

Path: The file or directory path, which can be the relative path in the sandbox.

Entries: Specify the files or subdirectories to be deleted under Path when it is a directory, which supports the wildcard * match.

Class: Specify the definition class of a callback method.

Selectors: Specify the Class methods that call the Class. Note that the methods must be class methods instead of instance methods.