Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 483
Josh Sherick Avatar asked Jun 16 '11 19:06

Josh Sherick


People also ask

When should I use Core Data vs UserDefaults?

Core Data only fetches the information it needs to perform the requests the application makes. This is very different from the defaults system. The UserDefaults class loads the property list into memory to improve performance and it asynchronously writes the changes back to disk at appropriate times.

Why should I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.


2 Answers

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.

like image 109
Tom Harrington Avatar answered Oct 26 '22 13:10

Tom Harrington


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!

like image 37
SushiGrass Jacob Avatar answered Oct 26 '22 13:10

SushiGrass Jacob