Local iPad database for iPad app

Discussion in 'iOS Programming' started by Deivore, Jan 8, 2014.

  1. Deivore, Jan 8, 2014
    Last edited by a moderator: Jan 8, 2014

    Deivore macrumors newbie

    Joined:
    Jan 8, 2014
    #1
    Hello all!

    I'm writing an educational game and want to log user actions on a local database since I am not guaranteed internet access at the deployment site.

    What I tried initially was to use a database path string as

    Code:
    dbPathString = [[NSBundle mainBundle] pathForResource:@"Journey" ofType:@"sqlite"];
    which wrote to a database file inside the application support files of the program. The program pulled from and pushed to the database and these changes were preserved in Xcode's iPhone Simulator. However, the deployed program on the iPad never seemed to make changes in the respective file on the iPad. I determined this by examining the ipad wih "iPad File Explorer".

    Am I doing something wrong here? Is there a better way to write and read from a database local to the iPad?

    Thanks!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  3. Deivore thread starter macrumors newbie

    Joined:
    Jan 8, 2014
    #3
    Ok, will do. Thanks.

    Edit: Looks like I was maybe trying to write to <Application_Home>/AppName.app when I should have written to <Application_Home>/Documents/ , be back soon.
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    Right. The app bundle is read-only on an iOS device (beware, you CAN write to files in the bundle from the simulator, so it appears to work from the sim.)

    What you want to do is write code that checks for an SQL file in your documents directory at launch. If it exists, open it and proceed as normal. Changes to documents will be saved correctly.

    If the SQL file does NOT exist, copy it from the bundle into your documents directory. Then fall into the code that opens it in the documents directory.

    That way, on first launch, you put the starting database into the documents directory, and after that you use the users updated database.
     
  5. Deivore thread starter macrumors newbie

    Joined:
    Jan 8, 2014
    #5
    Success!

    Wanted to report back, say that this was indeed the problem. Thanks all!
     
  6. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #6
    You should learn how to use the Core Data framework, because you can easily use it to store data on a device. The advantage of telling the app to cache the data is that the app can use data it retrieved the last time it synchronized the two databases.
     

Share This Page