Storing data locally

Discussion in 'Mac Programming' started by Monaj, Dec 30, 2010.

  1. Monaj macrumors regular

    May 24, 2009
    Hi all,

    In my application I am retrieving data from database and showing in it. In db some of the tables contain 1000+ records. Now my requirement is to show this data even if there is no net connection, so I am planning to store the tables in SQLite db at user's machine, but there is a worry:

    Since SQLite db will be included within resources folder, in project, so it will be by default contained within application binary. So I want to know that when the application will be launched will all this data reside on RAM ? If yes, then I think this can cause some problems, so in that case- is there any alternative solution to store data locally?


  2. jiminaus, Dec 30, 2010
    Last edited: Dec 30, 2010

    jiminaus macrumors 65816


    Dec 16, 2010
    Mac OS X will not automatically load everything in the resources directory into RAM. So don't be concerned about the SQLite database being in the resources directory.


    This isn't good practice. The best practise IMHO would be to have the database in ~/Library/Caches/Blah/, where Blah is a directory with the same name as the bundle identifier of your application (something like com.mycompany.myapp).

    Include an empty SQLite database in the resources folder as you planned, but don't put any data into it. Instead, when your application launches create the ~/Library/Caches/Blah directory and copy the SQLite database from your resources directory (if they don't exist already) and then use the database in the Caches subdirectory.

    BTW You can get the full, "future-proof" path to ~/Library/Caches with code like this:

    NSArray *array = 
    NSString *pathString = [array objectAtIndex: 0];
  3. jiminaus, Dec 30, 2010
    Last edited: Dec 31, 2010

    jiminaus macrumors 65816


    Dec 16, 2010
    Ahh, I've just read your original post more carefully.

    The files in the Resources folder inside your XCode project, don't end up in your application's executable. Mac OS X (and iOS for that matter) is different from other operating systems with respect to how applications are package.

    What you see as an application icon, in say /Applications, is not a single file, but a directory. If you control+click on these and choose "Show Package Contents" you can see the files within the application.

    The files in the Resources folder are copied in to the Contents/Resources directory within your application when XCode builds.

    Check out the Bundle Programming Guide in the developer docs. The Bundle Structure section goes into detail about the directories and files within a Mac OS X application. There's also a section titled Accessing a Bundle's Contents which will help you access the SQLite database file.

Share This Page