Looking for advice: Storing and retrieving data locally within an iPhone app

Discussion in 'iOS Programming' started by DDustiNN, Oct 1, 2012.

  1. DDustiNN macrumors 65816

    DDustiNN

    Joined:
    Jan 27, 2011
    #1
    Hello, I'm hoping someone here could give me a push in the right direction.

    I am still relatively new to iOS development, so I'm not sure of the exact terms I should be searching for. I want to make sort of a checklist app that contains a pre-determined list of roughly 50 items (the user will not be adding or removing items), and allow the user to check them off as they're completed (basically they'll have 2 states: isComplete = yes or no). In addition, I would like to allow the user to enter notes for each item (individually) if desired. There will also be an overall progress bar from 0-100%, but I guess I can figure that out later.

    I can work out the views and such, but I'm curious as to what would be the best way to store and access the data. The data must be local to the user's device (as I do not have a database server), and the data should persist when the app is closed (so everything is exactly how they left it next time the use the app).

    In my searching, I see lots of things mentioned... XML parsing, plist, JSON, SQLite, flat files, localStorage... but I'm having trouble discerning which would be the easiest and most efficient for my rather small set of data. I would also like the solution to be compatible with iOS 4, 5, and 6, without any separate configurations required depending on the iOS version (during my search I saw that localStorage was changed for iOS 5, or something along those lines). If the app data can be backed up to iCloud, then that is even better (though I have not even looked into that at all yet - still figuring out how to store it, haha).

    I hope this background makes sense to give you an idea what I'm looking for. If anyone could give me some hints/advice, or point me to an appropriate tutorial, I would very much appreciate it :D

    Thanks!
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Easy,
    Core Data is your answer, with a prepopulated database.
    You can let Core Data generate the database for you after you set your "Mapping Model", and then copy paste it, fill it with items, and then ship that with your application, and you're set, then it's just calling the Database and change the booleans.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    plist, sqlite, core data will all work for this. For a small amount of data the one to choose is up to you. You might base this on what you already know and which of these three seems easiest for you to learn. I would say that plist is easiest to learn followed by sqlite and then core data. Also, all developers should know how to use plists, sqlite and core data are more specialized skills that not every developer needs to know.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Heck, even NSUserDefaults might suffice in this situation.
     
  5. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #5
    Small flat data set, core data seems like massive overkill to me.
    Think dejo is on the money with NSUserDefaults. Or if willing to limit to iOS 5/6 then jump in to iCloud key value store. Sounds like you would be in the limits of that.
     
  6. DDustiNN thread starter macrumors 65816

    DDustiNN

    Joined:
    Jan 27, 2011
    #6
    Thanks for all of the replies so far.

    Could you guys recommend a good tutorial for any of these? I have no experience with any of them. I do have some experience with XML and SQL (from using C# and Java), but nothing yet in Obj-C and Cocoa.

    It sounds like plist is the best bet so far. Is that similar to XML? Where you just have your own structured data? I'd like to have each item have a few properties, some boolean and one text entry field for the user. It seems like this could work.

    Also if I go the iCloud route, will the data still be stored locally on the user's device? I would like the user to be able to use the app without requiring an internet connection.

    Thanks guys!
     
  7. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    A plist is falling-off-a-log simple.

    You're entire "object graph" needs to consist of property list objects (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects.)

    Usually you create a container object like an array or dictionary that contains other objects (sometimes in complex arrangements like an array of dictionaries that contain other dictionaries that contain arrays...)

    You can then use writeToFile:atomically: to save the dictionary or array to a file. To read the data back, you use dictionaryWithContentsOfFile, dictionaryWithContentsOfURL, arrayWithContentsOfFile, or arrayWithContentsOfURL.
     
  8. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    That chapter makes it sound harder than it is. Most often, the NSArray and NSDictionary writeToFile/ read from file methods are all you need.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    My point was that there's more to it than just throwing objects into an NSArray or NSDictionary. The objects must be from a limited set of types, and the dictionary keys must be NSString.

    I agree, it is often as easy as using the readFrom/writeTo file methods. However, understanding the limitations is always a good idea.
     

Share This Page