Which data model strategy to use for a relatively simple app? - iphone

For a relatively simple app that manipulates objects that store a date/time and some other values? Should I use property lists? archiving? or sqlite?

Plists are the simplest way to do save data. Use the built in saving methods in the collection classes (NSarray, NSDict, etc.)

You might want to take a look at NSUserDefaults.

Related

what us the best practice to create persistence array in IOS?

usually in Android I use </string-array name=""> tag in .xml file and refer to it inside the code by its name.
what is the best practice to create an array in IOS that I can use in multiple views without having to create it over and over again?
Depending on what values you want to store in your array and if you want to persist if you need to write into the array and persist it between app runs as well, you have several methods.
Store the array in UserDefaults. You can read from/write to UserDefaults from each of your ViewControllers and your data will be persisted even if the user quits the app.
Store it in a file. I wouldn't really recommend this option, unless you have a lot of data to store that can be easily represented as String/NSData.
Store data in a shared, singleton variable. This method is only suitable if you don't want to write into your array or you don't need to persist it between runs.
Use a database framework (CoreData or Realm). This is only recommended, if you have really complex data, but since you mentioned only an array, this shouldn't be your case.
Based on the amount of information provided in your question, I would suggest going for UserDefaults, it is quite easy to handle data in UserDefaults and it should be sufficient for your task.

Storing info locally without using NSUserDefaults

I was wondering whether there are any other ways in swift to save data locally on device, which will be available also after app close without using NSUserDefaults.
If there are, in which cases is that specific solution preferable?
I know this is kind of general, but I know just this one way to do saving locally and from my experience, there is always more than one way to do something(unless there isn't).
Any answers would be greatly appreciated
Yes there are different ways to store data locally.
As you have mentioned one of them is
NSUserDefaults
In addition to that you can also:
Use files such as plist, JSON or your own custom format.
Use Core Data
Use SQLite
And also you can combine between those.
Here some nice post Core Data VS SQLite
Now for:
in which cases is that speciffic solution preferable
I would say that NSUserDefaults is good when you need to store some small pieces of data, for example: Numbers, Booleans, Strings.

What are some advantages of using Core Data? (as opposed to plist)

I am relatively new to iOS and programming, and I made an app before, but it used a plist for storage, which I saved to the documents folder. Now, I am thinking about switching over to Core Data, but it looks a little complicated, and I'm not sure if it will work for what I want. I am going to have a bunch of data which I need to graph, so I'm not sure if Core Data is best for this, as it seems that I cannot create an array type in the .xcdatamodeld file. What are some other advantages of Core Data? Is it faster? Easier to use (once you set it up)?
Update: For anyone wondering, I finished the app, and it was totally worth it to learn how to use Core Data, and it was a lot less complicated that I originally thought. Doing it with plists would have been hell. The way they go about doing it seemed a little cryptic at first but if you just start using it you will get it. The relationships are really what is awesome about it.
A few advantages off the top of my head:
Much better memory management. With a plist you must load the entire thing into memory; with Core Data only the objects you're currently using need to be loaded. Also, once objects are loaded, they're normally placeholder "fault" objects whose property data doesn't load until you need it.
Related to the above, when you have changes, you can save only the changed objects, not the entire data set.
You can read/write your model objects directly instead of converting them to/from something like an NSDictionary.
Built-in sorting of objects when you fetch them from the data store.
Rich system of predicates for searching your data set for objects of interest.
Relationships between entities are handled directly, as properties on the related objects. With a plist you would need to do something like store an object ID for a relationship, and then look up the related object.
Optional automatic validation of property values.
Data models don't use arrays, but "to-many" relationships are modeled as sets.
It's a matter of what you're saving. For simple strings, arrays, dictionaries, it's fine to use a plist. For something more complicated (data, images, non-object information) or something with to-many relationships (think relationship between song to album, or photo to photographer), then something like a more robust solution might work better like SQLite.
CoreData is an objective-c-based wrapper around SQLite. If you think you might want to something more complicated, CoreData might be the way to go.
If you need a quick tutorial, I'd check out: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
This got me going and allowed me to learn the basics the workings of CoreData.
Good luck!

Store Data on Objective-C

I would simply like to create a file that stays around after the program quits. I would like it to contain objects that I have initiated, is that possible? Or does it just have to have text in it? How can I access that file later? Also, is this done the same way on the Mac and the iPhone?
Take a look at Core Data or SQLite. Apple has some sample code for them, not sure about SQLite because they prefer you tou use core data.
I will direct you to this question. Adding core data to my iPhone app
Or use NSUserDefaults: Best way to save data on the iPhone
Yes! It's possible.
Derive your objects that you want to persist from NSCoding. On those objects, implement the initWithCoder and encodeWithCoder functions on those objects. In these functions, write all of your primitive types to the coder (or read them) using the functions here http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSCoder_Class/Reference/NSCoder.html
When you want to save your file, use an NSKeyedArchiver and pass in the object you want to archive along w/ the file name.
[NSKeyedArchiver archiveRootObject:root toFile:string];
More examples here https://stackoverflow.com/questions/2805026/iphone-problem-in-saving-file-using-nscoding
I have used SQLitePersistentObjects in the past to give my Objective-C class objects an ORM-esque feel and behaviour. The objects itself translate into corresponding tables in the database, which in this case is a simple SQLite database file.
Archives and Serializations Programming Guide might be interesting as well.

iphone: Caching an NSMutableArray?

I'm currently writing a Twitter client for iPhone and need a way to cache the public timeline. The data fetched is stored as 'Status' objects in an NSMutableArray.
The NSMutableArray stores 20 objects at a time.
What is the best way to do this?
You will probably want to use Core Data. You'll need to create an entity for each tweet, and then store that in your database. There is quite a lot of documentation and sample code on Core Data out there; start at Apple's Samples page.
If you don't want to play with one of the frameworks already mentioned (SQLitePersistentObjects and CoreData), you could implement your own using the NSCoding protocol on your status objects. The array should already implement NSCoding for archiving/unarchiving.
If you want to start quickly try SQLitePersistentObjects.
If you never plan to go over 20 objects you could (slightly) violate some rules from the gods of apple, and use NSUserDefaults - likely the easiest one to implement, but as the name implies, made for things like user settings, so don't use for big piles of info.
Disclaimer: If you know how to use the others, use them, this one is kinda the lazy way if you don't want to learn something better but more difficult.