2

I have used NSCachesDirectory in my iPad App to store images and PDFs. Now, my client's complaint is that he is not able to see downloaded content after few hours of downloading. iPad has iOS 11.2.1.

In begging, my thought was that, there may be a low memory situation and thus, iOS system is deleting those files as these files are stored in NSCachesDirectory. But, surprisingly there is enough space in iPad i.e. 4 GB of available space and downloaded content are not more than 500 MB of size.

So, I have few questions related to this:

(1) Even though there is enough space in iPad, why system is deleting my files ?

(2) Is there may be an another reason why files are being removed from NSCachesDirectory ?

(3) If iOS system is deleting those files, what is the safest place on App memory to download those files ? I have found that NSCachesDirectory is the discardable memory so, that is not safest place.

(4) Will NSDocumentDirectory will be the best suitable option for my case ? or may be other option given by Apple ?

Here is the list of App directories:

typedef NS_ENUM(NSUInteger, NSSearchPathDirectory) {
    NSApplicationDirectory = 1,             // supported applications (Applications)
    NSDemoApplicationDirectory,             // unsupported applications, demonstration versions (Demos)
    NSDeveloperApplicationDirectory,        // developer applications (Developer/Applications). DEPRECATED - there is no one single Developer directory.
    NSAdminApplicationDirectory,            // system and network administration applications (Administration)
    NSLibraryDirectory,                     // various documentation, support, and configuration files, resources (Library)
    NSDeveloperDirectory,                   // developer resources (Developer) DEPRECATED - there is no one single Developer directory.
    NSUserDirectory,                        // user home directories (Users)
    NSDocumentationDirectory,               // documentation (Documentation)
    NSDocumentDirectory,                    // documents (Documents)
    NSCoreServiceDirectory,                 // location of CoreServices directory (System/Library/CoreServices)
    NSAutosavedInformationDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 11,   // location of autosaved documents (Documents/Autosaved)
    NSDesktopDirectory = 12,                // location of user's desktop
    NSCachesDirectory = 13,                 // location of discardable cache files (Library/Caches)
    NSApplicationSupportDirectory = 14,     // location of application support files (plug-ins, etc) (Library/Application Support)
    NSDownloadsDirectory API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 15,              // location of the user's "Downloads" directory
    NSInputMethodsDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 16,           // input methods (Library/Input Methods)
    NSMoviesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 17,                 // location of user's Movies directory (~/Movies)
    NSMusicDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 18,                  // location of user's Music directory (~/Music)
    NSPicturesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 19,               // location of user's Pictures directory (~/Pictures)
    NSPrinterDescriptionDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 20,     // location of system's PPDs directory (Library/Printers/PPDs)
    NSSharedPublicDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 21,           // location of user's Public sharing directory (~/Public)
    NSPreferencePanesDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 22,        // location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes)
    NSApplicationScriptsDirectory NS_ENUM_AVAILABLE(10_8, NA) = 23,      // location of the user scripts folder for the calling application (~/Library/Application Scripts/code-signing-id)
    NSItemReplacementDirectory API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 99,      // For use with NSFileManager's URLForDirectory:inDomain:appropriateForURL:create:error:
    NSAllApplicationsDirectory = 100,       // all directories where applications can occur
    NSAllLibrariesDirectory = 101,          // all directories where resources can occur
    NSTrashDirectory API_AVAILABLE(macos(10.8), ios(11.0)) API_UNAVAILABLE(watchos, tvos) = 102             // location of Trash directory

};
NSPratik
  • 4,714
  • 7
  • 51
  • 81
  • 1
    Caches may be deleted by system any time so don't save images there, NSDocumentDirectory is where you can save images and they will be deleted only when user delete application from his phone. p.s. from iOS 11 user can delete app, but keep documents and data. – Arthur Sahakyan Dec 15 '17 at 16:02
  • Thanks... Any idea on question no. 1? – NSPratik Dec 15 '17 at 19:27
  • Are you talking about `NSCache` or `NSCacheDirectory` ? – NSPratik Dec 16 '17 at 05:32
  • 1
    Store in Documents folder. dont in cache. cache is for cache and not for user data. cache means like downloaded thumbnails or others that are ok to delete (app will download again as needed). Better still, after store in doc folder, then sync to iCloud. – GeneCode Dec 18 '17 at 06:26
  • 1
    my answer is right for both, NSCache and NSCacheDirectory may be deleted by system. – Arthur Sahakyan Dec 18 '17 at 08:29

0 Answers0