Question

I have read the Data Storage guidelines of Apple and am really confused as to where i should keep the sqlite database files i am creating in my app. I want to read from the sqlite files even when the app is in Offfline mode. I read that such files created should be kept in the Library/caches with the "do not backup" flag set. Please suggest me the right approach for doing the same.

Was it helpful?

Solution

The answer depends on how your database files are created:

According to the Data Storage Guidelines page:

  1. Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the /Documents directory and will be automatically backed up by iCloud.

  2. Data that can be downloaded again or regenerated should be stored in the /Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.

This seems relatively clear to me: if your app programatically creates something or generates data that you want saved, put it in the "Caches" folder. If it's something unique that the customer themselves generate (via typing into a keyboard or something they manually intervened and did), put it in "Documents".

"Do Not Backup" means that the files never get sent up to iCloud and if they get lost, they have to be re-created from scratch (or re-install).

OTHER TIPS

This may help you. Copying the database to the documents directory will be good enough, The files on bundle is read only, In some situation If you need to update at anytime the better way is copying the database at first if it not exists on the documents directory.

The sample code for copying the database to the documents directory is follows:

-(void) checkAndCreateDatabase {

    // Setup some globals
    NSString *databaseName = @"AnimalDatabase.sql";

    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];


    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];

    // If the database already exists then return without doing anything
    if(success) return;

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    [fileManager release];
}

You can refer this Tutorial The files, contents on documents directory is read, write mode, so you can read the contents from the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top