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

2

21

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.

Retrocede answered 16/6, 2011 at 19:13 Comment(1)
You may find this answer on the difference between Core Data and SQLite to be helpful...Doubleheader
B
28

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.

Billposter answered 16/6, 2011 at 19:59 Comment(1)
+1. Love the way you explains things for each Question. #FanOfTom.Wavy
H
5

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!

Holub answered 16/6, 2011 at 19:59 Comment(1)
While true that Core Data uses SQLite (on the iPhone), that is an implementation detail and you shouldn't think about Core Data as just a wrapper. It's an object persistence framework. It handles saving to and restoring from disk for you.Embody

© 2022 - 2024 — McMap. All rights reserved.