How to make a NSMutableArray persist over time? - ios

I'm a brand-new iOS developer and have no previous experience with Objective-C, though I'm familiar with the C programming language and the Object-Oriented Programming paradigm.
To begin I followed the Start Developing iOS Apps Today tutorial and developed a basic to-do list app where each item is a NSMutableString object stored in a NSMutableArray. My app is working properly. However, my to-do list disappear when I quit and relaunch the app on the simulator. Obviously this happens because I provided no way to make the array persist over time.
Here there's a solution to this problem using NSUserDefaults, but I've seen that it's not an efficient solution, mostly because NSUserDefaults is made specifically for storing user's preferences and has very poor performance. Therefore I'm searching for another, still simple and straightforward solution to make my app more professional. Despite this fact, I'm having some trouble in understanding where (i.e. in which classes and methods) to put the code to achieve that.
Thank you in advance.

To make the data persist over time, I suggest you check out NSCoding and also CoreData. Both of them are simple to use and implement.
NSCoding example
CoreData example

Even if there are solutions like CoreData that are specific to solve this problem, you might want something more light and quick to implement, but still cleaner than saving junk in the User Defaults.
NSMutableArray is NSCoding compliant, so as long as the items it contains are NSCoding compliant as well, you can easily store it in a file and retreive it back with the methods
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
- (NSArray *)initWithContentsOfFile:(NSString *)aPath
Bear in mind that you might have problems if you're storing primitive types like boolean values, so I advice to save them with an NSNumber instead.

Related

How to store user data iOS

I need to store data that the user can add, they can add an unlimited amount. They can either be NSStrings or UIImages. I have looked into NSUserDefaults but it seems that it is used for small amounts of data such as settings or preferences.
What would be the best/most secure way to store the users information so that when they close the app it is still in the app. The data populates a UITableView and is a NSMutableArray.
Whats the best way to do this?
There must be a dozen ways to store user data in iOS. Here are several:
Property lists: An easy way to store a graph of common data storage objects and containers. This is a good place to start if you're just learning the iOS ropes.
NSKeyedArchiver and NSKeyedUnarchiver: Provides an easy way to serialize and deserialize your objects to/from a chunk of data, which you can then write/read using NSData's methods.
NSFileHandle: Read and write data in whatever format you like using a nice Objective-C API. More generally, you should read up on the iOS file system.
UIDocument: A full-featured starting point for managing user data, including syncing with iCloud.
Keychain: Not a general purpose data storage mechanism, but if you're storing sensitive items like passwords, credit card numbers, etc., you should use the keychain API.
POSIX file API: Good old C file handles with the read and write functions you learned in college, if you went to college before Java was a thing.
SQLite: According to the web site: "SQLite is the most widely deployed SQL database engine in the world."
Core Data: A powerful (but also somewhat complex object graph manager. This is a good choice if you have many different pieces of related data to store.
What would be the best/most secure way to store the users information
so that when they close the app it is still in the app. The data
populates a UITableView and is a NSMutableArray.
Best is subjective -- you'll need to consider your needs and look at the various options. For many people, though, best means least painful or easiest to learn. As mentioned above, property lists may be the way to go in that case. If your array contains simple data (strings, data, dates, numbers) in standard containers (arrays or dictionaries), your file I/O can be as simple as something like this:
// writing
[myArray writeToFile:somePath atomically:YES];
// reading
myArray = [[NSArray arrayWithContentsOfFile:somePath] mutableCopy];
You should use Core Data. There is a very good, free beginners course online avaibable called cs193p, see here http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/287, it is also available through iTunes U. It's really worth the time to watch and easy understandable.
If you have only some array you can check plist. is verry simple and powerful.
https://developer.apple.com/library/iOs/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html
This is a great resources: Should I use NSUserDefaults or a plist to store data?

best way to save Players details for offline game

Im working on a multiplayer turn based game in xcode for iphone/ipad im considering my options when it comes to where to save users details such as name / points ect. I was considering creating a plist and storing data in this also i have considered nsuserdefaults and also core data.
Basically majority of the time the user will be adding new details on every launch however this will not be the case 100% of the time.
What would you guys consider the best approach?
This really depends on multiple factors:
How much data is it going to be saved?
How fast the data needs to be loaded?
I suggest you create a serializable object, that conforms to NSCoding protocol. There is also an option to save this as JSON or like you noted, plist.
I think Core Data is too much boiler plate, if this is all you are storing. NSUserDefaults is fast enough and already prepared for you to use.
I don't think someone can give you the correct answer, I suggest you experiment with all the options and see which one is the best for you.

IOS Storing state of UI elements

I have an IOS app with multiple UIIMageView objects and wondering what would be the best way to remember their locations if the user killed the app or if they wanted to save that particular layout for future use.
I'm a beginner at this so looking for the simplest and most effective method. The only thing I can think of at the moment is to extract CGPoint location and tag of each object and save to NSUserDefaults or something but i'm hoping theres an easier way...
For something as simple as storing CGPoints, I'd recommend using NSUserDefaults, because you can very easy manage the data in it using a NSMutableArray.
You can go for Core Data and then maybe integrate iCloud, however NSUserDefaults keeps it simple.
Since your problem is to do with the state of the app, I'd recommend reading what Apple has to say about it.

How should I store Cocoa app data?

I'm just getting started on making a board game using Cocoa/Objective-C. My plan is to first get it functional as a Mac OS X app, and then port it to iOS.
I saw something in the documentation for the Core Data API that made it sound like it was the recommended way to store data persistently, and made reference to the fact that iOS apps should be able to quit and be restored in exactly the same state. I got the initial impression that I should plan to use Core Data for any variable that has to do with the game's state to support this.
But as I'm learning more it seems like my initial impression isn't correct. Core Data seems more like something intended to provide similar features as embedded SQL, and is more complicated than is required just for storing the game state persistently on disk. Is there a simpler way to support fast app restoring in iOS apps, or is Core Data the way to go?
Core Data is fantastic for storing lots of data of different types, including custom objects.
However, if you're talking about storing things like high scores for a game or other simple int, float, BOOL, NSString, NSArray data, then for iOS NSUserDefaults is a quick and easy way to go.
Core Data lets you store data as objects, so if your game state can be described with native data types, Core Data just might work for you. You'd essentially be manipulating object states.
If you're looking for something a little more lightweight, look into the NSUserDefaults API (which is the same thing that the Settings App on iOS uses).
Alternatively, you could come up with your own format and use that, serializing your own data to disk.
I'd start with NSUserDefaults.

Best way to save data in iPod Touch/iPhone Objective-C

I am writing a very simple application, for the iPhone. Unfortunately I am really a newbie.
What I am trying to do is to save data at the end of a user experience. This data is really simple, only string or int, or some array.
Later I want to be able to retrieve that data, therefore I also need an event ID (I suppose).
Could you please point out the best way, API or technology to achieve that, XML, plain text, serialization... ?
Use NSUserDefaults. Straight forward and easy to use. This will handle all File I/O for you, and takes only a couple lines of code.
NSUserDefaults is a good choice for small amounts of data.
If you need to manage something larger than a kilobyte or so, you might consider using the initWithContentsOfFile: methods of NSArray or NSDictionary to read in a .plist file. To write the file, use the writeToFile:atomically: methods. This file needs to go in your app's documents directory.
Me and my team created a simple class for this purpose which as Mark pointed out makes use of NSUserDefaults. Hopefully this helps you out...
http://getsetgames.com/2009/10/07/saving-and-loading-user-data-and-preferences/

Resources