need to save some persistent data. the data would only contain 3 or 4 arrays with 100-200 single value entries.
(array) data
(
(
(dictionary) key -> value
)
(
(dictionary) key -> value
(
//etc
)
these are just simple lists to show in pickers.
so far i've used NSUserDefault to store single dictionary values, and i'm wondering if it would suffice for this new data or do i need to consider using Core Data?
at this point in time, i don't see my program needing to sore more data then this.
NSUserDefaults shouldn't have any trouble with the data usage you described.
That said, it doesn't mean it's the ideal solution. NSUserDefaults are really intended to deal with user preferences. So, if these values aren't preferences NSUSerDefaults isn't really the place for them. (not that it won't work)
CoreData might be a little overkill for your situation, but it might be worth the intial learning curve to have that skill under your belt.
Another solution would be to simply use NSArray or NSDictionary classes if your data is consistant.
NSUserDefault doc - http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
Really depends on task.
You should answer on these questions:
How often this data should be changed?
How often this data should be queried ?
What kind of queries do you want to use on this data?
As far as I see, if you just need to show them in pickers, you obviously can use NSUserDefaults, or plist file to save this data between launches.
And NSArray/NSDictionary to use it in app.
How often is this accessed or changed? I would guess that NSUserDefaults is fine.
Related
NSOrderedSet seems to be able to give O(1) lookup speed of hash-tables and array like ordering of objects? What is the data structure used to achieve this? Is it a combination of two, e.g: Hashtable and a separate Array where index i has the key corresponding to it's object in the Hashtable?
We don't know how Apple chose to implement this data structure. The only way to find out would be to reverse-engineer the Foundation framework. But this is not an useful thing to do, Apple could change the implementation and underlying data structure with every update. So relying on this for a production app would be very stupid since it could break the app at any time.
If you wanted to implement this yourself your approach with a hash table and an array will work. The best way would be to store the objects in the array and have the hash map store the array indices keyed by objects.
Of course there are other possible ways how this could be implemented with different performance characteristics. It could be just an array making the containsObject: test O(n), or it could just be a hash table (with the object as key and index as value) making the objectAtIndex: operation O(n).
My program should save, retrieve and manipulate a large load of data. (Around 200,000 entries, which all have around 20 entries.)
I'm wondering how I should implement the data: create my own class, use a struct or use NSDictionary.
The whole data would be saved in an array (for going through the whole data). But since performance matters (particularly the searching part) I would like to chose the fastest and savest way.
Any suggestions?
CoreData with sqlite store type is the way. You'd be able to fetch portions of data without reading the whole thing to the memory at once. This way you'd be able to generate custom NSManagedObject subclasses if you'd need custom behavior for each instance of an object.
I am building a very simple app to store data on people like name, age, etc. I give the user the option of changing the data such as when a person has a birthday. I can't figure out the best way to store the data.
I have searched for a long time now and I can't find the simple answer that I am looking for. If I change the data in a mutable array will that data be changed permanently? I mean will that write over the existing code and change the data so that it will still exist when the app is closed and reloaded again?
So far I am not able to do that. I have experience in MySql and Sql server so I have a tendency to go that direction if possible, but I would really like to use a mutable array or mutable dictionary if possible. This may be such an elementary question, but I can't find an answer anywhere.
You have some misconceptions.
The objects you create are in memory. There's nothing permanent about them. You have to save them somehow or they are gone when you quit the application and come back.
If you want to save an array, you have a number of options.
If the array contains nothing but objects of type NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary, you can save the array using the system class NSUserDefaults.
NSArray also has a method writeToFile:atomically: that will save an array of data to a file.
If your array contains any objects other than the types I listed above, though, neither of those approaches (NSUserDefaults or writeToFile:atomically) won't work.
The other option is to use an NSKeyedArchiver to convert the contents of your array to data, and then write that data to a file. In order for that approach to work, every single object i your array, and all the objects in those objects, need to support the NSCoding protocol.
As others have pointed out, you could also use Core Data or mySQL to save your data, but that seems like overkill for just saving an array.
Take a look at Core Data. It is the easiest way to manage this kind of data storage requirement on iOS.
Take a look at my book, it got good reviews and is perfect for what you are trying to do: http://www.amazon.com/Pro-Core-Data-iOS-Professionals/dp/1430233559
If you want an easy way to get started, there are tons of online tutorials too. For example Ray has written some good stuff:
http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started
Mutable means you can change the data at any time, so no, it's not permanent.
Yes, it will be permanent in terms of what you are asking. Although because the array is mutable it means you can always change the data back, the initial data will be overwritten by this new data.
Edit: No, the information will not remain after closing the app. You will need to programmatically save it. I would use Core Data.
CoreData is the way data is normally persisted in Cocoa and Cocoa-Touch. It also gives you some nice extras like undo support. But it has a big learning curve.
If what you're doing is super-simple, look at NSUserDefaults. If you need a little more flexibility, you could always use NSArray's -writeToFile:atomically: and -initWithContentsOfFile: (there are also versions of both those methods that take URLs instead of file paths).
Anything more complicated than that, and it's probably worth the trouble learning CoreData.
I've been reading up on saving objects, preserving state etc, but I'm still a little confused on the route I should take when saving objects that are created from my app. I have built an app where a user can create a Radio Station with the Genre, name and frequency. Once a user hits save, a "RadioStation" object is created using a custom class and is stored in an NSMutableArray that is placed in the RootViewController. While RadioStation objects can be stored in this array and are displayed correctly in the table view, it's obvious that once the application quits, these are no longer going to be held by the application.
At this point I'm not sure how I should begin to architect this design. Do I need to set up a plist? Should I use Core Data with a SQLite DB? Do I need to research iOS object serialization for both these scenarios? Or is there an more straight forward way that I'm not aware of?
There are three ways to persist data locally:
Store objects in a database using managed objects or SQLite
Store objects in a file in the sandbox using various classes' writeToFile
Store the data in NSUserDefaults
How you persist your object data depends on how you're using it, and what type of data you're saving. CoreData makes sense if you have a lot of objects of the same type and SQL's search and sorting mechanisms would be useful. I tend to use the sandbox for very large files, eg images, sound files, and large agglomerations of data bounced through NSData. And I use UserDefaults for small amounts of data that might be considered "preferences".
You can put anything anywhere you want, and I suggest being familiar with all three mechanisms when you start a new project. Additional techniques, like using NSCoding to serialize and deserialize classes, can be used to make saving data easier. Note that once you've implemented NSCoding, you could store data anywhere and even send it over the wire.
For your RadioStation, I would personally use UserDefaults. The benefit of using NSCoding is that, once you implement initWithCoder and encodeWithCoder, you can stream a hierarchy of objects (eg an array of custom objects, each of which might contain a dictionary, array, or custom object, etc) fairly easily. If the only thing you're saving is an array of strings, then directly using UserDefaults is easy enough.
The most powerful approach is to make your custom object a subclass of NSManagedObject, and store it in an NSManagedObjectContext using Core Data. On iOS this will always use an sqlite database, on Mac OS X you can use sqlite, xml, or a proprietary binary format (smaller/faster than xml).
Depending on how complicated your data structure is, and how likely it is to change in future, this technique may or may not be too complicated for your app. It's a lot of work to setup, and the API is very complicated, but it's the best choice if you need to store tens of thousands/millions of objects in a file.
For your app, I would have a single sqlite file containing all of the radio stations in the app.
The simplest option, is to implement the NSCoding protocol in your custom object's class. Documented here:
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Archiving/Articles/codingobjects.html#//apple_ref/doc/uid/20000948-BCIHBJDE
The basic theory is your object writes or reads all it's data to a "coder", and then passes the coder on to any child object(s). Outside of the class, you use an "archiver" or an "unarchiver" class to coordinate everything.
Once your custom class implements NSCoding you can read/write it to the disk with:
myObject = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
[NSKeyedArchiver archiveRootObject:myObject toFile:filePath];
This is a very simple approach, but it has a few drawbacks, for example you will need to figure out your own way to support different versions of the file format (perhaps with a different file extension for example).
Many classes in Cocoa/CocoaTouch already implement NSCoding, so they can be written to a file in this fashion (including NSArray, NSString, NSNumber, etc).
For your app, i would use this to store each radio station in a single file. When the app launches, load all the radio stations from a directory. You should consider enabling iTunes drag/drop filesharing for your app, so users can easily share radio station files with their friends or between devices.
This would also fit in well with iCloud for syncing between devices, I haven't looked into iCloud much, but I expect one file per radio station will be the ideal way to implement iCloud syncing.
No need for such things. If you only have like one thing to save, then research this method of NSArray:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
I your user can save many radio stations, then you might want to look into CoreData, which has a bit of a curve, but ultimately will be the easiest way.
Hi could anyone point me in the right direction with a tutorial, guide or sample code, thanks, Sami.
The answer given by shreyasva is close but somewhat misleading.
First, parsing the XML into an easily-managed Cocoa data structure is perfectly correct. For performance reasons, you shouldn't be tying your table's datasource directly to the XML. yan.kun's suggestion is certainly possible but if you have "more than a little" data, you very well could run into performance problems. I highly recommend just parsing the data into an NSArray of NSDictionary objects for longer data sets.
Second, Core Data is a bit overkill if you don't plan to persist the XML document in some other way or if you only have a handful of objects. Overkill by a long shot. It's also not necessary (and often not reasonable) to shoehorn every data structure in your app into Core Data without good reason. An NSDictionary instance will work just fine for caching the parsed data for consumption by a table view.
Third, there is no -tableView:cellForRowAtIndexPath: method. This seems to be confusing NSTableView with UITableView. Since you specified the Mac tag, look into the NSTableViewDataSource protocol. Cocoa Bindings is not "better than" or a "replacement for" the data source protocol. It's an "alternative to". You can either load your parsed data into an NSArrayController (an array of dictionaries, one per "record", for example) and bind the table columns to it (each column is bound to a key in the dictionaries in the array controller's arrangedObjects) or just use the (easy) table data source protocol that takes literally two minutes of copy/paste from the docs to get up and running.
Ill give you an idea of the architecture.
Parse the XML using any popular XML parser NSXMLParser is fine.
Store the data using core data objects, if data is not too much, keep it in memory.
Load data in tableView:cellForRowAtIndexPath:
Alternatively, if you just want to list the content, without editing capabilities, you can load the XML into a NSXMLDocument and bind the TableView via xPath.