Should I write a sqlite database file in the Documents or Library / Caches directory?

I read the Apple Storage Guide and am very confused about where I have to store the sqlite database files that I create in my application. I want to read from sqlite files, even if the application is in Offfline mode. I read that the created files should be stored in the library / caches with the "do not backup" flag set. Please suggest me the right approach for this.

+5
source share
2 answers

The answer depends on how the database files are created:

According to the data storage manual page :

  • , , /Documents iCloud.

  • , , /Library/Caches. Caches - , , , , .

: - , , "". , ( -, ), "".

" " , iCloud, , ( ).

+5

. . . , - , .

:

-(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];
}

Tutorial , , , .

+1

All Articles