简体   繁体   中英

Where to store developer generated data for IOS applications

I need to create some text files with large amounts of data that will be required to run an app. Now I thought that the Library folder would be ideal for storing such data. I programatically created sub folders(where I intend to store the data files) to the library folder, within the viewDidLoad method of the app's main view. So here's my code for creating one of the sub-folders

    let filemgr = NSFileManager.defaultManager()
    let dirPaths = NSSearchPathForDirectoriesInDomains(.LibraryDirectory,.UserDomainMask, true)
    let libDir = dirPaths[0] as! String

    let newDir = (libDir as NSString).stringByAppendingPathComponent("subFolder1")

    //Create the sub-dir else throw error
    do {
        try filemgr.createDirectoryAtPath(newDir, withIntermediateDirectories: false, attributes: nil)
    } catch let error as NSError {
        NSLog("Unable to create directory \(error.debugDescription)")
    }

My sub folder is successfully created when I run this code, as evident from it being listed when I list the directories in Library folder. Now, I delete this piece of code(since it has already served its purpose), and attempt to build the app again on another device, but the new subfolder isn't listed on this new device as I expected. I thought that once created, any file/folder in the Library folder would persist within that folder(similar to files created in the app bundle) and would be available to any device on which the app is installed. Apparently I was wrong. So where do I store user created files/folder which I expect to be available on every device on which the app is installed? Thanks for your thoughts

Assuming you can't include these files in the app bundle then Library/Application Support/ is probably the right place for them. You're free to create your own subdirectory there but will need to check for it's existence and create it as needed on every device.

As part of your app launch process check to see if the directory and files you need exist and create/update them as needed. Keep in mind that your app could launch and encounter data in Application Support from a previous version of the app (if the user just installed an update) so you probably want to repeat this check every time the app launches in case you make changes to the set of required data files between app versions.

Since you can apparently re-create this data you'll also want to exclude it from user backups. See the File System Programming Guide

  • Put app-created support files in the Library/Application Support/ directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle.
  • Remember that files in Documents/ and Application Support/ are backed up by default. You can exclude files from the backup by calling -[NSURL setResourceValue:forKey:error:] using the NSURLIsExcludedFromBackupKey key. Any file that can be re-created or downloaded must be excluded from the backup. This is particularly important for large media files. If your application downloads video or audio files, make sure they are not included in the backup.

You mean that if the user installs your app on an iPad, adds some records, then installs the same app on their iPhone, you want the newly installed records to show up on the iPhone?

In that case you need to use iCloud.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM