Should i write sqlite database file to Documents directory or Library/Caches?
Asked Answered
C

2

6

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.

Codycoe answered 7/8, 2012 at 6:19 Comment(0)
H
6

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).

Henka answered 7/8, 2012 at 6:30 Comment(4)
I have developed a app fully static . I am storing data more than 4 mb of size. With out this content the app will not work. In my case where should i save the local database(documents folder or library folder)Wasteland
Well I see you've already asked this as a separate question, so either I (or somebody else) will go look up the answer for youHenka
What about new Extensions and Watch ?Derm
I am not sure, but I think for a single app, depending on your database's requirements you would may be reading some parts of your app from Documents, some parts from Cache and some parts in tmp simultaneouslyHoneycutt
R
1

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.

Regression answered 7/8, 2012 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.